Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: January 28, 2016 - 6:18 pm UTC

Last updated: April 17, 2018 - 1:57 am UTC

Version: Oracle SQL Developer 1.5.5

Viewed 10K+ times! This question is

You Asked

Hello,

I'm fairly new, but I have been finding bits and pieces on Julian date conversion, but not a full explanation of the Julian date conversion?

I.E TO_NUMBER(TO_CHAR(SYSDATE, 'YYYYDDD'))-1900000

Firstly, the SYSDATE is using the TO_CHAR function (to convert to a VARCHAR2 = 'YYYYDDD').
1. Can one explain why the format 'YYYYDDD' is used?
2. SYSDATE appears as '28-JAN-16' when I run a query on SYSDATE. Would this not be considered VARCHAR already?

Secondly, the results of the TO_CHAR function is then converted again to TO_NUMBER
1. What is the meaning and purpose of '-1900000'?

Thanks in advance.


and Connor said...

I'm curious - where did you see:

TO_NUMBER(TO_CHAR(SYSDATE, 'YYYYDDD'))-1900000

because I agree with you, that seems a very strange expression if it's dealing with dates. The only thing I can think of, is that some tools may want a number in that form for their own internal date processing. *Without* the subtraction, its known as the Ordinal date format:

"Ordinal dates: YYYY-DDD or YYYYDDD
An ordinal date is a simple form for occasions when the arbitrary nature of week and month definitions are more of an impediment than an aid, for instance, when comparing dates from different calendars. "

Source: https://en.wikipedia.org/wiki/ISO_8601

With respect to: "SYSDATE appears as '28-JAN-16' when I run a query on SYSDATE"

Internally a date is 7 bytes of data, so when you query it in (say) SQL Plus, it will be converted to a string before being presented to you. But this is a feature of the tool doing the query. Inside the database, its a date of 7 bytes - so in your application *code* you would store sysdate directly into a date variable (and not do to_char etc).

Hope this helps.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

JDEdwards Julian Date

Aubrey, February 02, 2016 - 12:17 am UTC

A quick Web search for his SQL seems to indicate that it returns the JDEdwards Julian Date. A search for that would likely yield more information as to what it is used for.
Connor McDonald
February 02, 2016 - 1:44 am UTC

Thanks for the input.

Julian date

Wawa Yu, April 16, 2018 - 10:57 pm UTC

Sysdate is a DATE, '01-JAN-16' is only a default format display. All DATEs are kept as an integer in the background, Julian day 1 being 1, day 2 being 2, etc.

To convert today's date to a julian date, simply do:

select to_char(sysdate,'j') from dual;

it will show today's Julian date, probably in the 2 millions.

Connor McDonald
April 17, 2018 - 1:57 am UTC

"All DATEs are kept as an integer in the background"

If you are referring to other technologies, then maybe. But Oracle dates are not stored like this.

Oracle dates are 7 bytes:

century
year
month
day
hour
min
second

and they are offset to avoid zeros, ie, we use an internal format to represent that information, so it is not really storing 20, 05, 06, 25, 12, 01, 00 for June 25, 2005, at 12:01:00. Using the built-in DUMP function, we can see what Oracle really stores:

SQL> create table t ( x date );

Table created.
 
SQL> insert into t (x) values
  2  ( to_date( '25-jun-2005 12:01:00',
  3             'dd-mon-yyyy hh24:mi:ss' ) );
1 row created.
 
SQL> select x, dump(x,10) d from t;
 
X         D
--------- -----------------------------------
25-JUN-05 Typ=12 Len=7: 120,105,6,25,13,2,1


The century and year bytes (the 120,105 in the DUMP output) are stored in an excess-100 notation. You would have to subtract 100 from them to determine the correct century and year. The reason for the excess-100 notation is support of BC and AD dates.