Convert Julian Date to YYYY-MM-DD

后端 未结 8 1041
心在旅途
心在旅途 2021-01-06 16:11

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

相关标签:
8条回答
  • 2021-01-06 16:51

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

    0 讨论(0)
  • 2021-01-06 16:52

    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

    0 讨论(0)
  • 2021-01-06 16:53

    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'))
    
    0 讨论(0)
  • 2021-01-06 16:53

    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)
    )
    
    0 讨论(0)
  • 2021-01-06 16:54

    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

    0 讨论(0)
  • 2021-01-06 17:04

    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))
    
    0 讨论(0)
提交回复
热议问题