简体   繁体   中英

Convert unix time format to date/time format in oracle sql

I have a column called data_time (varchar) and there are about 200 thousand rows. I would like to convert those rows to ordinary date/time instead.

I have tried this with no luck.

example value in a row: 927691200000000

SELECT * TO_DATE('19700101',yyyymmdd') + ((date_time/1000)/24/60/60) thedate 2 FROM table1

I am new to SQL and help is appreciated!

Thank you.

I cleaned up the obvious syntax errors, added some date formatting, and just hardcoded the one sample value you provided, thus:

SELECT to_char(TO_DATE('19700101','yyyymmdd') + ((927691200000000/1000)/24/60/60),'DD-MON-YYYY') thedate  FROM dual;

And that yielded:

ERROR at line 1:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0

Which suggests that your unix time is (probably?) expressed in microseconds, not milliseconds.

So, I modified the query thus:

SELECT to_char(TO_DATE('19700101','yyyymmdd') + ((927691200000000/1000000)/24/60/60),'DD-MON-YYYY') thedate  FROM dual;

Which returns:

THEDATE
-----------
26-MAY-1999

Which I assume to be correct?

Hope that helps....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM