Convert 7 digit Julian Date to DateTime in SQL?

时光怂恿深爱的人放手 提交于 2019-12-12 03:55:41

问题


I have seen numerous examples on the web and a few here on SO. However, none seem to work for my data. Here are a few julian dates with their known converted values.

JUL | DateTime
2454522 | 2008-02-25 00:00:00.000
2454571 | 2008-04-14 00:00:00.000
2455713 | 2011-05-31 00:00:00.000

I have tried this which does not work:

DECLARE @JD int = 2454522
SELECT DATEADD(YEAR, @JD / 1000 - 1900, @JD % 1000 - 1)

It gives me this: 2455-06-06 00:00:00.000 which is obviously wrong. What am I missing here? Thanks ind advance.


回答1:


One approach that should work in SQLServer:

select @jd, dateadd(d,@jd - 2440588,'1970-01-01')

SQLFiddle here.




回答2:


As long as your DBMS supports some set of scalar functions capable of computing the number of days between two dates and can add a number of days to a given date, all you need to do is compute some sort of normalization value for Julian Dates and the do some fairly simple date math.

In a DB/2 environment the relevant functions are: DAYS and DATE. start with some base date you know the Julian date of. For example the Julian date for 2000-01-01 is: 2451545.

Next use the DAYS/DATE scalar functions to compute an integer value of the same date. The query to do this in DB/2 is:

select days(date('2000-01-01'))
from sysibm.sysdummy1
;

The result of this query is: 730120

Use these two values to compute a normalization factor for Julian Dates: 2451545 - 730120 = 1721425

Now you can compute the Gregorian Date from an Julian Date as follows:

select date(juliandate - 1721425)
from sysibm.sysdummy1
;

Using the examples from your question:

select date(2454522 - 1721425),
       date(2454571 - 1721425),
       date(2455713 - 1721425)
from sysibm.sysdummy1
;

Returns the following dates: 2008-02-25 2008-04-14 2011-05-31

Your DBMS may not support the specific scalar functions used in the above example, however, most will support some mechanism of adding some number of days from a Gregorian Date and determining the number of days between two dates. You should be able to devise some workable formula using these functions and a normalization factor for Julian Dates as illustrated above.




回答3:


These are Astronomical Julian Date values, whole days only. The quick and dirty conversion to datetime in SQL Server (which of course cannot go below the year 1753) would be:

DECLARE @jd as int = 2454522
SELECT CAST(@jd - 2415021 as datetime)

2008-02-25 00:00:00.000

2415021 comes from the following:

SELECT CAST(0 as datetime)

1900-01-01 00:00:00.000

And according to the at Julian Date converter, 1900-01-01 CE is 2415020.5. Add 0.5 to that because the astronomical Julian Date starts from noon, and we are dealing with whole days only.

If you do not like dirty, try this one:

DECLARE @jd as int = 2454522
SELECT DATEADD(DAY, @jd - 2415021, '1900-01-01')

2008-02-25 00:00:00.000


来源:https://stackoverflow.com/questions/16279457/convert-7-digit-julian-date-to-datetime-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!