SQL Server:将儒略日期转换为 YYYY-MM-DD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33550260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Server : convert Julian Date to YYYY-MM-DD
提问by Mochael_DLite
I have searched far and wide, but I can't seem find a way to convert julian to yyyy-mm-dd
.
我已经进行了广泛的搜索,但似乎找不到将 julian 转换为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.
Julian 格式由年份(前两位数字)和一年中的日期(最后三位数字)组成。
For example, 95076
is March 17, 1995
. The 95
indicates the year and the
076
indicates it is the 76th day of the year.
例如,95076
是March 17, 1995
。该95
表示年份和
076
表明它是一年中的第76天。
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
回答by MikeVe
You can select each part of the date using datepart()
您可以使用选择日期的每个部分 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))
回答by Dmytro Shevchenko
Edit: leaving this answer for Oracle and MySQL users
This will not work in T-SQL.
编辑:为 Oracle 和 MySQL 用户留下这个答案
这在 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
.
这返回March, 17 1995 00:00:00
.
回答by Mochael_DLite
I concatenated 20 to my JD and then ran
DATEADD(YEAR, LAST_CHANGE_DATE / 1000 - 1900, LAST_CHANGE_DATE % 1000 - 1)
我将 20 连接到我的 JD 然后运行
DATEADD(YEAR, LAST_CHANGE_DATE / 1000 - 1900, LAST_CHANGE_DATE % 1000 - 1)
this got me the result. Thank you!!!
这让我得到了结果。谢谢!!!
回答by Mitul Panchal
FOR SQL UsersDECLARE @jdate VARCHAR(10)
SET @jdate = 117338
SELECT dateadd(dd, (@jdate - ((@jdate/1000) * 1000)) - 1, dateadd(yy, @jdate/1000, 0))
对于 SQL 用户DECLARE @jdate VARCHAR(10)
SET @jdate = 117338
SELECT dateadd(dd, (@jdate - ((@jdate/1000) * 1000)) - 1, dateadd(yy, @jdate/1000, 0))
回答by Mitul Panchal
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'))