Home > Back-end >  Convert strange 5 digit number to normal Date in Oracle SQL
Convert strange 5 digit number to normal Date in Oracle SQL

Time:02-01

I have numbers like 42946 in a database column and i want to format it as a normal Date like dd/mm/yyyy. I was searching a lot but i dont find anything usefull.

I tried to do TO_DATE(<date>,'J') but this doesn't work beacuse i think 42946 is the number of days between today and January 1 of 1900, and this method works only for dates that are between today and January 1 of 4712 BC.

I hope you can help me with this.

CodePudding user response:

i think 42946 is the number of days between today and January 1 of 1900

You mean, number of days since 1st of January 1900?

If so, you don't have to do much - just add that value to date you specified and you'll get the result. Its (result's) datatype is DATE so you can display it any way you want, using the to_char function (or any other option you prefer):

SQL> select date '1900-01-01'   42946 as result from dual;

RESULT
----------
01.08.2017

SQL>

Example of formatting it:

SQL> select to_char(date '1900-01-01'   42946, 'dd-mon-yyyy', 'nls_date_language = english') as result from dual;

RESULT
-----------
01-aug-2017

SQL>
  • Related