将日期转换为Julian日期,然后存储在SQL Server 2005中的数字字段中

use*_*579 5 t-sql sql-server-2005

我在T-SQL变量中有一个日期字段,我想将其转换为Julian日期格式,然后插入到Numeric表中的列中.有人可以帮忙吗?

我看到Julian Date上有一些帖子,但我无法通过.

EX:

declare @d datetime 
Set @d = GetDate()
select datediff(d, 0, @d) + 693596
Run Code Online (Sandbox Code Playgroud)

上述语句确实转换为julian日期,但格式不正确.例如,如果今天是2014年2月15日,那么这应该将其转换为114046,但它将其转换为735279.

一旦这被转换为正确的朱利安格式.我想将其转换为Numeric,因为我想插入numeric表中的列.

如果以前曾问过这个问题,我很抱歉.我是SQL Server 2005的新手.

任何帮助将不胜感激

问候

Guy*_*ery 6

放手一搏:

DECLARE @input_date DATETIME

SELECT @input_date = getdate()

SELECT datepart(year, @input_date) * 1000 + datepart(dy, @input_date)
Run Code Online (Sandbox Code Playgroud)


小智 6

上面的答案似乎对我不起作用。

--If Julian Format is defined as CYYJJJ where: 
--  C is the number of centuries since 1900-01-01 
--  YY is the 2 digit year 
--  JJJ is the day number of the given YY year

--Convert Date => Julian uning CYYJJJ

declare @date datetime = '02/15/2014' 
select (datepart(year, @date)-1900)*1000 + datepart(dy, @date) 
--output: 114046

--Convert Julian => Date using CYYJJJ

declare @jdate int = 114046 
select dateadd(dd, (@jdate - ((@jdate/1000) * 1000)) - 1, dateadd(yy, @jdate/1000 - 1900, 0))
--output: '02/15/2014'

---

--Convert Date => Julian uning YYYYJJJ

declare @dateB datetime = '02/15/2014' 
select (datepart(year, @dateB))*1000 + datepart(dy, @dateB) 
--output: 114046

--Convert Julian => Date using YYYYJJJ
declare @jdateB int = 2014046 
select dateadd(dd, (@jdateB - ((@jdateB/1000) * 1000)) - 1, dateadd(yy, @jdateB/1000 - 1900, 0)) 
--output: '02/15/2014'
Run Code Online (Sandbox Code Playgroud)

我建议把它放到一个标量函数中,这样你就可以像这样执行它:

select dbo.FromJulianDate(2014046)
Run Code Online (Sandbox Code Playgroud)

来自http://www.sqlservercentral.com/Forums/Topic778671-169-1.aspx