SQL JULIANDAY function
Returns the date as a REAL count of days since November 24, 4714 BC; subtract two Julian days and you get the days between them.
- Returns
REAL (days)- In this category
- 53 of 121
语法
JULIANDAY('2024-01-15')
JULIANDAY('now')
julianday(d2) - julianday(d1)
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | Not built in | DATEDIFF(day, d1, d2) for day counts |
| PostgreSQL | Different name or syntax | to_char(d, 'J') gives the integer Julian day as text |
| MySQL | Not built in | DATEDIFF(d2, d1) for day counts; TO_DAYS for a day number |
| SQLite | 是 | Supported under this name. |
| Oracle | Different name or syntax | TO_CHAR(d, 'J') gives the integer Julian day |
示例
SQLite
SELECT julianday('2024-02-01') - julianday('2024-01-15') AS days_between;
SQLite
SELECT CAST(julianday('now') AS INTEGER) AS julian_day;
Oracle
SELECT TO_CHAR(SYSDATE, 'J') AS julian_day FROM dual;
值得了解
- The number includes the time as a fraction: julianday('2024-01-15 12:00') is .5 past the midnight value; CAST to INTEGER for pure day counts.
- It is not an epoch: Julian days count from 4714 BC, not 1970. strftime('%s') is the seconds-since-1970 form.
- Unparseable input returns NULL silently, so a malformed date string looks like a missing value rather than an error.
Questions about JULIANDAY
What is a Julian day number?
A continuous count of days since 4714 BC used by astronomers: no months, no zones, just days, which is why date subtraction is trivial.
How is it different from Unix time?
Julian days count days (with fractional time) from 4714 BC; Unix time counts seconds from 1970. julianday('1970-01-01') is 2440587.5.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQLite, PostgreSQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.