I have a string field ‘ShippedDate’ in the following format (1998-04-06 0:00:00) that I need to convert to datetime format. I have tried using DATE and CAST functions, but because there are NULL values in the field I am getting the following error: Invalid timestamp: ‘NULL’.
I tried using those functions with IFNULL (see below), but I’m still getting the same error. How do I get around this issue? Thanks!
SELECT IFNULL(CAST(ShippedDate AS DATETIME),null)
SELECT IFNULL(DATE(ShippedDate),null)
>Solution :
You can use SAFE_CAST instead.
SELECT SAFE_CAST(ShippedDate AS DATETIME);