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

空扰寡人 提交于 2019-12-01 00:13:34

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