I recently worked on a database where a date & time variable was stored as text in a VARCHAR type, in multiple different formats (don't ask...), and had to convert it to a TIMESTAMP type.
Since there is no TO_TIMESTAMP()
function in Redshift, I used the trick suggested by Yiyu Jia on his [blog][1]. In a nutshell, the trick is to
- use TO_DATE() to get the date converted
- append the time portion of the input text to the above
- CAST the resulting string to a TIMESTAMP
For example here's the snippet to deal with a field named myDate with dates in either of the following formats
- "Feb 8 2013 10:06PM"
- "25/09/2007 16:21:00"
It is rather heavy, but works. A regex test is used to test if the date corresponds to the format handled on a given line. (that is only necessary when dealing with multiple possible formats)
The 'Feb 0 2013' case is a bit more complicated because I remove the time portion of the text, before submitting it to TO_DATE(), and because another regex is used to extract the time portion that is appended (as opposed the simpler SUBSTRING() used for the same purpose, in the other case).
... ,
CASE
-- Special date indicating "date not available": replaced by NULL
WHEN myDate = '31/12/9999 23:59:59' OR myDate = 'Dec 31 9999 11:59PM' THEN NULL
-- 'Feb 8 2013 10:06PM' case
WHEN myDate ~ '^[JFMASOND][a-z]{2}' THEN
CAST(TO_DATE(REGEXP_REPLACE(myDate , '\\s[0-9]{1,2}:[0-9]{2}[AP]M$', ''), 'Mon FMDD YYYY') || REGEXP_REPLACE(myDate , '[JFMASOND][a-z]{2}\\s+[0-9]{1,2}\\s+[0-9]{4}\\s+', ' ') AS TIMESTAMP)
-- '25/09/2007 16:21:00' case
WHEN myDate ~ '^[0-9]{2}/[0-9]{2}/[0-9]{4} ' THEN
CAST(TO_DATE(myDate , 'DD/MM/YYYY HH24:MI:SS') || SUBSTRING(myDate FROM 11) AS TIMESTAMP)
ELSE NULL
END AS MyNiceTimeStamp,
...
[1]: http://yiyujia.blogspot.com/2014/04/redshift-convert-integer-to-timestamp.html