0

I'm trying to copy a table to the columns of a different table but the date fields are not valid(Date are like '00000000') for some of the columns so I'm trying to check if the date is valid and I'm trying to set it NULL if so. Example,

INSERT INTO table1 (ID, FirstName, LastName, @BD) SELECT * FROM table2 SET Birthday = IF(@BD = '00000000', NULL, CAST(@BD as DATE)) 

This format works for LOAD while loading data from a CSV file but gives and error saying the syntax is wrong. Also, table2 columns are in this order ID, FirstName, LastName, and Birthday. Thanks in advance!

3
  • you have missed values in insert statement Commented Sep 19, 2018 at 13:38
  • What is the syntax error ? Please edit your question to include the error details! Commented Sep 19, 2018 at 13:39
  • SET more more commonly used with UPDATE.. MySQL supports INSERT INTO ... SET ... but i advice you never to use it because it's not a valid ANSI SQL insert query. Commented Sep 19, 2018 at 14:00

1 Answer 1

3

Have you tried converting during select instead

INSERT INTO table1 (ID, FirstName, LastName, Birthday) SELECT ID, FirstName, LastName, CASE BirthDay WHEN '00000000' THEN NULL ELSE CAST(BirthDay as DATE) END FROM table2 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.