Opposite of dayofyear

  • I have a problem of converting character column that contains a year and a number of days into the datetime format.

    Sample data - 2003260

    The following statement produces 260:

    select datepart(dayofyear, '09/17/2003')

    I need the opposite function to interpret 2003260 into 09/17/2003.

    Any ideas?

    Thanks

  • Try something like this:

    declare @jdate char(7)

    set @jdate = '2003260'

    select convert(char(10),

    dateadd(day,cast(substring(@jdate,5,7) as int)-1,cast(substring(@jdate,1,4) + '-01-01' as datetime))

    , 101)

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • SELECT DATEADD(d,RIGHT(@d,3)-1,LEFT(@jd,4))

  • Thanks guys,

    Both solutions seem to work.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply