MySQL LOAD DATA Trick

I leaned a new trick today with LOAD DATA INFILE . I’m migrating some data from an external source, and the Date Format is not the MySQL required YYYY-MM-DD, it was DD-MMM-YY. So how do you load this into a Date Field.

`
$ echo “02-FEB-07” > /tmp/t1.psv
$ mysql -umysql
USE test;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1(d1 DATE);

echo “02-FEB-07” > /tmp/t1.psv

LOAD DATA LOCAL INFILE ‘/tmp/t1.psv’
INTO TABLE t1 (@var1)
SET d1=STR_TO_DATE(@var1,’%d-%M-%y’);
SELECT * FROM t1;
EXIT
`

The trick is to bind the appropriate column within the file being loaded to a variable, @var1 in my example and use the SET syntax to perform a function on the variable. Rather cool.

A good tip to know.

Tagged with: Databases MySQL

Related Posts

AWS CLI support for Aurora DSQL and S3 Tables

If you were following the AWS Re:invent keynote yesterday there were several data specific announcements including Aurora DSQL and S3 Tables . Wanting to check them out, I downloaded the latest AWS CLI 2.

Read more

Migrating off of WordPress - A Simplified Stack

The ongoing drama between Wordpress v WP Engine continues to cross my reading list, but I have permanently removed WordPress from my website. I have finally transitioned away from the complex Linux/Apache/MySQL/PHP (LAMP) stack required for self-hosting WordPress on my professional website.

Read more

WeSQL Introduction – MySQL running on S3

I recently became aware of WeSQL . A MySQL-compatible database that separates compute and storage, using S3 as the storage layer. The product uses a columnar format by default which is significantly more space-efficient than InnoDB.

Read more