How to convert YYYY_WW in order to compare with a datetime column

  • Hi guys,

    I have a column StartDate in datetime format and another one called SessionDate in the format YYYY_WW. Somehow I need to get a date from YYYY_WW, so I can calculate the difference between the two dates in years.

    So, to replicate this:

     declare @StartDate datetime
    Set @StartDate=DATEADD(dd, DATEDIFF(dd,0,DATEADD(YEAR,-2,GETDATE())), 0)
    select @StartDate as StDate

    declare @YearWk varchar(7)
    Set @YearWk='2020_10'
    select @YearWk as YearWk

    Somehow I need to create a column showing the difference between @StartDate and @YearWk in the number of years. I would appreciate any help. Thanks.

  • My first thought would be to simply take the difference of the year.  Do you want to adjust based on the month as well as the year?


    SELECT LEFT(@YearWk, 4) - YEAR(@StartDate) AS diff_in_years

    • This reply was modified 3 years, 3 months ago by  ScottPletcher.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • If StartDate was 2018-12-31 and SessionDate was 2020_01, what would be your desired answer as the number of years between the two?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Yes, I do want to take the month into consideration.

  • DaVinci_Cat wrote:

    Yes, I do want to take the month into consideration.

    What do you mean by this?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin wrote:

    If StartDate was 2018-12-31 and SessionDate was 2020_01, what would be your desired answer as the number of years between the two?

    So, 2018-12-31 to 2020-01 is not full 2 years, so I would like to see 1.

  • OK. One simplified solution would be as follows:

    1. Assume SessionDate is always the first of the month (so 2020-01-01 in my example above).
    2. Calculate the number of days between the two dates
    3. Divide the result by 365 and discard the decimal part of the result.

    The existence of leap years makes this an inaccurate solution in some cases, but would it be accurate enough for your purposes?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Or take the month difference and divide by 12.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • Is WW the week number - or month number?  If week number - is it ISO week or US week?

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

  • Jeffrey Williams wrote:

    Is WW the week number - or month number?  If week number - is it ISO week or US week?

    It's a calendar week number, as in the first week of January 2020 would be 2020_01 etc.

  • This is the solution I finally went with.  I'll be honest, there is a bit of cut-and-paste from Stackoverflow, which I don't entirely understand, but it is working fine.

    DATEDIFF(year,StartDate, DATEADD(week, CAST(REPLACE(RIGHT(session_date,2),'_','') as int), 
    DATEADD (year, CAST(LEFT(session_date, 4) as int)-1900, 0)) - 4 -
    DATEPART(dw, DATEADD(week, CAST(REPLACE(RIGHT(session_date,2),'_','') as int),
    DATEADD (year, CAST(LEFT(session_date, 4) as int)-1900, 0)) - 4) + 1
    ) as Tenure

    • This reply was modified 3 years, 3 months ago by  DaVinci_Cat.

Viewing 13 posts - 1 through 12 (of 12 total)

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