4

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 consists of the year, the first two digits, and the day within the year, the last three digits.

For example, 95076 is March 17, 1995. The 95 indicates the year and the 076 indicates it is the 76th day of the year.

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
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mochael_DLite
  • 167
  • 2
  • 5
  • 14

9 Answers9

9

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))
MikeVe
  • 1,062
  • 8
  • 13
1

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

Dmytro Shevchenko
  • 33,431
  • 6
  • 51
  • 67
0

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!!!

Mochael_DLite
  • 167
  • 2
  • 5
  • 14
0

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

Mitul Panchal
  • 592
  • 5
  • 7
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'))
Mitul Panchal
  • 592
  • 5
  • 7
0

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))
Obsidian
  • 3,719
  • 8
  • 17
  • 30
0

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

  • 1
    This question was answered four years ago. While your answer isn't identical to the accepted answer, it is quite close. Can you edit your answer to provide an explanation for why this approach is preferred over the accepted answer? Or, at least, under what conditions a reader might want to use your approach? Thank you. – Jeremy Caney Oct 03 '20 at 18:16
0

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)
)
klediooo
  • 630
  • 1
  • 7
  • 25
0

Try this out:

DECLARE @jdate int

SET @jdate = 21243

select dateadd(dd, (@jdate - ((@jdate/1000) * 1000)) - 1, dateadd(yy, @jdate/1000+100, 0))
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83