简体   繁体   中英

Convert Julian Date to YYYY-MM-DD

I have searched far and wide, but I can't seem find a way to convert julian to yyyy-mm-dd .

Here is the format of my julian:

The Julian format consists of the year, the first two digits, and the day within the year, the last three digits.

For example, 95076 is March 17, 1995 . The 95 indicates the year and the 076 indicates it is the 76th day of the year.

15260

I have tried this but it isn't working:

dateadd(d,(convert(int,LAST_CHANGED_DATE) % 1000)-1, convert(date,(convert(varchar,convert(int,LAST_CHANGED_DATE) /1000 + 1900) + '/1/1'))) as GrgDate

You can select each part of the date using datepart()

SELECT DATEPART(yy, 95076), DATEPART(dy, 95076)

+++EDIT: I misunderstood something. Here's my correction: +++++

SELECT DATEADD(day, CAST(RIGHT('95076',3) AS int) – 1, CONVERT(datetime,LEFT('95076',2) + '0101', 112))

Edit: leaving this answer for Oracle and MySQL users
This will not work in T-SQL.

Use this:

MAKEDATE(1900 + d / 1000, d % 1000)

For example:

SELECT MAKEDATE(1900 + 95076 / 1000, 95076 % 1000)

This returns March, 17 1995 00:00:00 .

SQLFiddle

I concatenated 20 to my JD and then ran DATEADD(YEAR, LAST_CHANGE_DATE / 1000 - 1900, LAST_CHANGE_DATE % 1000 - 1)

this got me the result. Thank you!!!

FOR SQL 用户DECLARE @jdate VARCHAR(10) SET @jdate = 117338 SELECT dateadd(dd, (@jdate - ((@jdate/1000) * 1000)) - 1, dateadd(yy, @jdate/1000, 0))

This will definitely work in all case.

DECLARE @date int
SET @date = 21319
SELECT DATEADD(dd, RIGHT(@date,LEN(@date)-3)-1, DATEADD(yy,LEFT(@date,1)*100 +RIGHT(LEFT(@date,3),2),'1 Jan 1900'))

You need to specify 1 or 0 for century in first character. For example, SET @date = 21319 should be prefixed with 1 or 0. Below is an example that will work with all y2k use cases.

DECLARE @jdate INT 
SET @jdate = 119150 
SELECT DATEADD(dd, (@jdate - ((@jdate/1000) * 1000)) - 1, DATEADD(yy, @jdate/1000, 0))

Declare @Julian varchar(7)

Declare @date date

Set @Julian = 2020277

Set @Date = Dateadd(day,+ Cast(right(@Julian,3) as int)-1, Cast(left(@Julian,4) + '0101' as Date))

Select @Date

Standard SQL is simple (you can do similar for 2 digit year YYMMDD)

Declare @julDate int = 2020275

Select 
DateAdd
(
   day
   , Right(@julDate,3)-1
   , Cast((Left(@julDate,4)+'-01-01') as smalldatetime)
)

Try this out:

DECLARE @jdate int

SET @jdate = 21243

select dateadd(dd, (@jdate - ((@jdate/1000) * 1000)) - 1, dateadd(yy, @jdate/1000+100, 0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM