MySQL Forums
Forum List  »  Newbie

combining fields when loading data
Posted by: Tom Peters
Date: December 09, 2012 07:56AM

Hi,
I'm trying to read a CSV text file into a database. Complication is that none of the fields is a unique key, which I believe my cause problems.
The input data on each row contain a date and an hour, which combination is unique. So I try to combine them into 1 DATETIME field, like this:

mysql> describe Data;
+-------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| STN | smallint(3) unsigned | YES | | NULL | |
| YMDH | datetime | NO | PRI | NULL | |
| HH | smallint(2) unsigned | YES | | NULL | |


mysql> LOAD DATA INFILE '/tmp/some.txt' REPLACE INTO TABLE Data FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 34 LINES (STN, @YMD, @HH) set YMDH = STR_TO_DATE( CONCAT(@YMD, ' ', CAST( (CAST(@HH AS UNSIGNED) - 1 ) AS CHAR(2)) , '%Y%m%d %H:00:00' ) ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

The error is not in the separator string ' ': if I replace it by e.g. '-' then I get the same error.

The following more or less works, but gives non-unique rows:
LOAD DATA INFILE '/tmp/some.txt' REPLACE INTO TABLE Data FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 34 LINES (STN, @YMD, HH) SET YMDH = STR_TO_DATE( @YMD, '%Y%m%d' ) ;

How should the top SQL statement look like?
Thanx,
Tom

Options: ReplyQuote


Subject
Written By
Posted
combining fields when loading data
December 09, 2012 07:56AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.