Code · SQL

Julian date in SQL

Convert dates to Julian dates and back in every major database: the day-of-year code (YYYYDDD, YYDDD), the JD Edwards CYYDDD format stored in ERP tables, and the astronomical Julian Day Number and Julian Date.

  • Day-of-year code: the year plus the day number 1–365/366, 2026202 (YYYYDDD) or 26202 (YYDDD). July 21, 2026 is day 202.
  • Julian Day Number: the astronomical day count. The JDN for July 21, 2026 is 2461243.

'J' is not the day of year

In Oracle and PostgreSQL, to_char(d, 'J') returns the Julian Day Number (2461243), not the day of the year. Use 'DDD' for the day and 'YYYYDDD' for the code. Parsing a CYYDDD or YYYYDDD value with 'J' gives dates thousands of years in the past.

Cheat sheet by database

d is a date column and c an integer YYYYDDD code. Each expression returns 2026202, July 21, 2026 or 2461243.

Database Date → YYYYDDD YYYYDDD → date Julian Day Number
SQL Server YEAR(d) * 1000 + DATEPART(dayofyear, d) DATEADD(day, c % 1000 - 1, DATEFROMPARTS(c / 1000, 1, 1)) DATEDIFF(day, '2000-01-01', d) + 2451545
Oracle TO_NUMBER(TO_CHAR(d, 'YYYYDDD')) TO_DATE(TO_CHAR(c), 'YYYYDDD') TO_NUMBER(TO_CHAR(d, 'J'))
PostgreSQL to_char(d, 'YYYYDDD')::int to_date(c::text, 'YYYYDDD') to_char(d, 'J')::int
MySQL / MariaDB DATE_FORMAT(d, '%Y%j') MAKEDATE(c DIV 1000, c MOD 1000) TO_DAYS(d) + 1721060
SQLite strftime('%Y%j', d) date(printf('%04d-01-01', c / 1000), '+' || (c % 1000 - 1) || ' days') CAST(julianday(d) + 0.5 AS INTEGER)
Db2 (LUW, IBM i) YEAR(d) * 1000 + DAYOFYEAR(d) DATE(DIGITS(DEC(c, 7, 0))) JULIAN_DAY(d)
BigQuery FORMAT_DATE('%Y%j', d) DATE_ADD(DATE(DIV(c, 1000), 1, 1), INTERVAL MOD(c, 1000) - 1 DAY) UNIX_DATE(d) + 2440588
Snowflake YEAR(d) * 1000 + DAYOFYEAR(d) DATEADD(day, MOD(c, 1000) - 1, DATE_FROM_PARTS(FLOOR(c / 1000), 1, 1)) DATEDIFF(day, '1970-01-01'::date, d) + 2440588
Spark SQL / Databricks year(d) * 1000 + dayofyear(d) date_add(make_date(c div 1000, 1, 1), c % 1000 - 1) datediff(d, date'1970-01-01') + 2440588

SQL Server (T-SQL)

DATEPART(dayofyear, d) (short form dy) gives the day number. Integer arithmetic builds and splits the code, and DATEFROMPARTS plus DATEADD turns it back into a date:

DECLARE @d date = '2026-07-21';

-- Day of year (1-366)
SELECT DATEPART(dayofyear, @d);                     -- 202

-- YYYYDDD as a number, and as zero-padded text
SELECT YEAR(@d) * 1000 + DATEPART(dayofyear, @d);  -- 2026202
SELECT FORMAT(@d, 'yyyy') + FORMAT(DATEPART(dayofyear, @d), '000');  -- '2026202'

-- YYDDD as text (keeps the leading zero for 2000-2009)
SELECT RIGHT('0' + CAST(YEAR(@d) % 100 AS varchar(2)), 2)
     + RIGHT('00' + CAST(DATEPART(dayofyear, @d) AS varchar(3)), 3);  -- '26202'

-- YYYYDDD back to a date (SQL Server 2012+)
DECLARE @c int = 2026202;
SELECT DATEADD(day, @c % 1000 - 1, DATEFROMPARTS(@c / 1000, 1, 1));  -- 2026-07-21

-- YYDDD back to a date: 00-49 = 2000s, matching SQL Server's default cutoff
DECLARE @s int = 26202;
SELECT DATEADD(day, @s % 1000 - 1,
       DATEFROMPARTS(CASE WHEN @s / 1000 < 50 THEN 2000 ELSE 1900 END + @s / 1000, 1, 1));

Julian Day Number and Julian Date

SQL Server has no Julian Day function, so anchor to a known value: January 1, 2000 is JDN 2451545.

DECLARE @t datetime2 = '2026-07-21T18:00:00';   -- a UTC date and time

-- Julian Day Number of the date
SELECT DATEDIFF(day, '2000-01-01', @t) + 2451545;      -- 2461243

-- Julian Date with the time of day (00:00 = .5, 12:00 = .0)
SELECT DATEDIFF(day, '2000-01-01', @t) + 2451544.5
     + DATEDIFF(second, CAST(@t AS date), @t) / 86400.0;  -- 2461243.25

-- Now, from the server's UTC clock
SELECT DATEDIFF(day, '2000-01-01', SYSUTCDATETIME()) + 2451544.5
     + DATEDIFF(second, CAST(SYSUTCDATETIME() AS date), SYSUTCDATETIME()) / 86400.0;

Oracle

TO_CHAR and TO_DATE understand DDD directly, so the codes take one call each:

-- Date to day-of-year codes
SELECT TO_CHAR(DATE '2026-07-21', 'DDD')     FROM dual;  -- '202'
SELECT TO_CHAR(DATE '2026-07-21', 'YYYYDDD') FROM dual;  -- '2026202'
SELECT TO_CHAR(DATE '2026-07-21', 'YYDDD')   FROM dual;  -- '26202'

-- Codes back to a date
SELECT TO_DATE('2026202', 'YYYYDDD') FROM dual;              -- 2026-07-21
SELECT TO_DATE(LPAD(26202, 5, '0'), 'RRDDD') FROM dual;      -- 2026-07-21
-- RR reads 00-49 as 2000-2049 and 50-99 as 1950-1999 (in this century)

Julian Day Number and Julian Date

-- 'J' is the Julian Day NUMBER (an integer), not the day of year
SELECT TO_CHAR(DATE '2026-07-21', 'J') FROM dual;      -- '2461243'
SELECT TO_DATE('2461243', 'J') FROM dual;              -- 2026-07-21

-- Julian Date with the time of day, for a DATE value in UTC
SELECT (d - DATE '2000-01-01') + 2451544.5 AS jd
FROM (SELECT TO_DATE('2026-07-21 18:00', 'YYYY-MM-DD HH24:MI') AS d FROM dual);  -- 2461243.25

-- Now, in UTC regardless of the session time zone
SELECT (CAST(SYS_EXTRACT_UTC(SYSTIMESTAMP) AS DATE) - DATE '2000-01-01') + 2451544.5 FROM dual;

Oracle’s 'J' uses the Julian calendar before October 15, 1582, the same convention as astronomers, so it matches published Julian Day Numbers for historical dates too.

PostgreSQL

-- Date to day-of-year codes
SELECT EXTRACT(DOY FROM DATE '2026-07-21');            -- 202
SELECT to_char(DATE '2026-07-21', 'YYYYDDD');          -- '2026202'
SELECT to_char(DATE '2026-07-21', 'YYDDD');            -- '26202'

-- Codes back to a date
SELECT to_date('2026202', 'YYYYDDD');                  -- 2026-07-21
SELECT to_date(lpad(26202::text, 5, '0'), 'YYDDD');    -- 2026-07-21 (00-69 = 2000s)
SELECT make_date(2026202 / 1000, 1, 1) + (2026202 % 1000 - 1);  -- 2026-07-21, integer input

Julian Day Number and Julian Date

-- Julian Day Number
SELECT to_char(DATE '2026-07-21', 'J')::int;           -- 2461243
SELECT to_date('2461243', 'J');                        -- 2026-07-21

-- Julian Date with the time of day. EXTRACT(julian ...) counts from midnight,
-- so subtract 0.5 to get the astronomical value, which starts at noon.
SELECT EXTRACT(julian FROM TIMESTAMP '2026-07-21 18:00') - 0.5;   -- 2461243.25
SELECT EXTRACT(julian FROM now() AT TIME ZONE 'UTC') - 0.5;        -- JD now

MySQL and MariaDB

%j in DATE_FORMAT is the zero-padded day of year, and MAKEDATE(year, dayofyear) goes the other way:

-- Date to day-of-year codes
SELECT DAYOFYEAR('2026-07-21');                        -- 202
SELECT DATE_FORMAT('2026-07-21', '%Y%j');              -- '2026202'
SELECT DATE_FORMAT('2026-07-21', '%y%j');              -- '26202'

-- Codes back to a date
SELECT MAKEDATE(2026202 DIV 1000, 2026202 MOD 1000);   -- 2026-07-21
SELECT MAKEDATE(IF(26202 DIV 1000 < 70, 2000, 1900) + 26202 DIV 1000,
                26202 MOD 1000);                       -- 2026-07-21

-- Julian Day Number (TO_DAYS counts from year 0)
SELECT TO_DAYS('2026-07-21') + 1721060;                -- 2461243

-- Julian Date now (UNIX_TIMESTAMP is UTC-based in any session time zone)
SELECT UNIX_TIMESTAMP(NOW(6)) / 86400 + 2440587.5;

SQLite

SQLite is the one database with the astronomical Julian Date built in: julianday() returns it as a real number, and every date function accepts one.

-- SQLite has the astronomical Julian Date built in
SELECT julianday('2026-07-21');                        -- 2461242.5
SELECT julianday('2026-07-21 18:00');                  -- 2461243.25
SELECT julianday('now');                               -- JD now (UTC)
SELECT julianday('2026-07-21') - 2400000.5;            -- MJD 61242.0
SELECT date(2461242.5);                                -- '2026-07-21'

-- Day-of-year codes
SELECT strftime('%j', '2026-07-21');                   -- '202'
SELECT strftime('%Y%j', '2026-07-21');                 -- '2026202'
SELECT date(printf('%04d-01-01', 2026202 / 1000),
            '+' || (2026202 % 1000 - 1) || ' days');   -- '2026-07-21'

JD Edwards (CYYDDD) dates in SQL

Oracle JD Edwards stores dates as CYYDDD: centuries since 1900, a two-digit year and the day of year. Adding 1900000 turns the value into a normal YYYYDDD code, so each database can reuse its own conversion. Values for the 1900s are five digits (99365) because the column is numeric; the arithmetic handles them without padding.

-- JD Edwards CYYDDD to a date, and back. 126202 = 2026-07-21, 99365 = 1999-12-31.
-- JDE stores "no date" as 0: wrap these in CASE WHEN jde = 0 THEN NULL ... END.

-- Oracle
TO_DATE(TO_CHAR(jde + 1900000), 'YYYYDDD')
TO_NUMBER(TO_CHAR(d, 'YYYYDDD')) - 1900000

-- SQL Server (JDE columns are often numeric, so cast to int first)
DATEADD(day, CAST(jde AS int) % 1000 - 1, DATEFROMPARTS(1900 + CAST(jde AS int) / 1000, 1, 1))
(YEAR(d) - 1900) * 1000 + DATEPART(dayofyear, d)

-- Db2 for IBM i (AS/400) and Db2 LUW
DATE(DIGITS(DEC(jde + 1900000, 7, 0)))
(YEAR(d) - 1900) * 1000 + DAYOFYEAR(d)

-- PostgreSQL
to_date((jde + 1900000)::text, 'YYYYDDD')
(EXTRACT(YEAR FROM d)::int - 1900) * 1000 + EXTRACT(DOY FROM d)::int

-- MySQL / MariaDB
MAKEDATE(1900 + jde DIV 1000, jde MOD 1000)
(YEAR(d) - 1900) * 1000 + DAYOFYEAR(d)

-- SQLite
date(printf('%04d-01-01', 1900 + jde / 1000), '+' || (jde % 1000 - 1) || ' days')
(CAST(strftime('%Y', d) AS INTEGER) - 1900) * 1000 + CAST(strftime('%j', d) AS INTEGER)

JDE keeps the time of day in a separate numeric HHMMSS column:

-- JDE time-of-day columns (e.g. UPMT) hold HHMMSS as a number: 143005 = 14:30:05
-- SQL Server: combine a JDE date and time into a datetime
DATEADD(second,
        (t / 10000) * 3600 + (t / 100 % 100) * 60 + t % 100,
        CAST(DATEADD(day, jde % 1000 - 1, DATEFROMPARTS(1900 + jde / 1000, 1, 1)) AS datetime2))

To check single values, use the JDE Julian date converter.

Worked example: July 21, 2026

  • Day of year: 202, so 2026202 / 26202
  • JD Edwards: 126202
  • Julian Day Number: 2461243
  • Julian Date at 00:00 UTC: 2461242.5

Most databases apply the Gregorian calendar to every date, so their Julian Day Numbers differ from the astronomical ones before October 15, 1582. Oracle is the exception: it switches to the Julian calendar before that date, as astronomers do.

Related

Frequently asked questions

How do I convert a Julian date to a date in SQL?

Split the number into the year (code / 1000) and the day of year (code % 1000), then add the day count to January 1. In SQL Server: DATEADD(day, c % 1000 - 1, DATEFROMPARTS(c / 1000, 1, 1)). Oracle and PostgreSQL can parse it directly with TO_DATE(code, 'YYYYDDD'), and MySQL has MAKEDATE(year, dayofyear).

How do I convert a JD Edwards Julian date in SQL?

Add 1900000 to the CYYDDD value to get YYYYDDD, then convert that. In Oracle: TO_DATE(TO_CHAR(jde + 1900000), 'YYYYDDD'). In SQL Server: DATEADD(day, jde % 1000 - 1, DATEFROMPARTS(1900 + jde / 1000, 1, 1)). Versions for Db2, PostgreSQL, MySQL and SQLite are in the JD Edwards section above.

What does TO_CHAR(date, 'J') return?

In Oracle and PostgreSQL, the J format element is the Julian Day Number, a 7-digit astronomical day count (2461243 for July 21, 2026). It is not the day of the year. Use DDD for the day of the year and YYYYDDD for the 7-digit code.

How do I get today's Julian date in SQL?

Apply the date-to-code expression to the current date: YEAR(GETDATE()) * 1000 + DATEPART(dayofyear, GETDATE()) in SQL Server, TO_CHAR(SYSDATE, 'YYYYDDD') in Oracle, to_char(current_date, 'YYYYDDD') in PostgreSQL, DATE_FORMAT(CURDATE(), '%Y%j') in MySQL, or strftime('%Y%j', 'now') in SQLite. The result follows the server or session time zone.