osbt
osbt

Reputation: 131

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.

Upvotes: 1

Views: 5672

Answers (1)

Mark J. Bobak
Mark J. Bobak

Reputation: 14385

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....

Upvotes: 3

Related Questions