SQL Server : convert Julian Date to YYYY-MM-DD

 ̄綄美尐妖づ 提交于 2019-12-19 03:56:59

问题


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

回答1:


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))



回答2:


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




回答3:


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!!!




回答4:


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




回答5:


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'))


来源:https://stackoverflow.com/questions/33550260/sql-server-convert-julian-date-to-yyyy-mm-dd

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