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
FOR SQL Users
DECLARE @jdate VARCHAR(10)
SET @jdate = 117338
SELECT dateadd(dd, (@jdate - ((@jdate/1000) * 1000)) - 1, dateadd(yy, @jdate/1000, 0))
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
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'))
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)
)
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
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))