问题
I have a requirement to convert MM/DD/YYYY to YYYYMMDD in amazon redshift database.
My result of this query gives me some weird result. Can some one please help me.
select to_date ('07/17/2017','YYYYMMDD');
0007-07-20
回答1:
If you just wish to convert the hard-coded string into a DATE
:
select to_date('07/17/2017', 'MM/DD/YYYY')
If you have a column already formatted as DATE
, then use:
to_char(fieldname, 'YYYYMMDD')
Combining the two concepts:
select to_char(to_date('07/17/2017', 'MM/DD/YYYY'), 'YYYYMMDD')
回答2:
TO_DATE - converts a date represented in a character string to a DATE data type.
TO_CHAR - converts a time stamp or numeric expression to a character-string data format.
select to_char(sysdate,'YYYYMMDD');
If I’ve made a bad assumption please comment and I’ll refocus my answer.
来源:https://stackoverflow.com/questions/45149631/convert-mm-dd-yyyy-to-yyyymmdd-in-redshift