5 years or older

  • Guys, if i have a list of dates in a table how do i query how many of the dates are 5 years or older?

  • craig.jenkins - Tuesday, February 20, 2018 4:38 AM

    Guys, if i have a list of dates in a table how do i query how many of the dates are 5 years or older?

    DATE functions could be used for your filtration.

  • Create your WHERE something like:

    WHERE  [your_date_column] > DATEADD (year, -5 , GETDATE() ) 

    Try to not wrap the function around your column so the query can still use any indexes on that column.

    ** Don't mistake the ‘stupidity of the crowd’ for the ‘wisdom of the group’! **
  • craig.jenkins - Tuesday, February 20, 2018 4:38 AM

    Guys, if i have a list of dates in a table how do i query how many of the dates are 5 years or older?

    Do you want five years ago today, or 1/1/five years ago? Is your date column actually a datetime column, in which case you might want to 'set' the time to 00:00.

    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.

  • Thank Guys, i've attached some dummy data below.  As you can see the regdates are not older than 5 years

    -- Sample data
    IF OBJECT_ID('tempdb..#LOGFILE') IS NOT NULL DROP TABLE #LOGFILE
    SELECT * INTO #LOGFILE FROM (VALUES
    ('BAA', '2018-02-19 10:53:08.837','2016-03-31 00:00:00.000','123451'),
    ('BAA', '2018-02-19 10:53:08.837','2015-02-28 00:00:00.000','123452')

    ) d (Dealer, Created, Regdate, RegNo)

    SELECT DISTINCT * FROM #LOGFILE
    WHERE #LOGFILE.Dealer = 'BAA'--
    AND #LOGFILE.Created >='2018-02-19' AND #LOGFILE.Created <'2018-02-20'
    AND #LOGFILE.RegdATE > DATEADD (year, -5 , GETDATE() )

  • Thank Guys, i've attached some dummy data below. As you can see the regdates are not older than 5 years

    And that is why you are selecting them.

    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.

  • I'm an idiot, sorry guys

  • craig.jenkins - Tuesday, February 20, 2018 5:50 AM

    I'm an idiot, sorry guys

    Don't be so hard on yourself, we've all done it 🙂

    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.

Viewing 8 posts - 1 through 7 (of 7 total)

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