locked
Get date exactly 30 days before RRS feed

  • Question

  • User-1901014284 posted

    Hi,

    I have a stored procedure which retrieves all dates 30 days or less on an expiry date but I am wondering if it is possible to only return values where the date is only 30 days days before? I would only like the records displayed that are exactly 30 days before the expiry date rather than display all dates less than 30 days before also.

    Any help would be greatly appreciated.

    Thanks

    Jonny

    Wednesday, September 12, 2018 9:02 AM

All replies

  • User-1901014284 posted

    My apologies, I have managed to do this by using the below code within MSSQL:

    SELECT *
    FROM TableName
    WHERE ExpiryDAte = CAST(DATEADD(d, 30, GETDATE()) AS DATE)

    Wednesday, September 12, 2018 10:00 AM
  • User-1952463932 posted

    Minor tweak to jonnygareth30's response; -30 not 30.

    Thursday, September 27, 2018 7:20 AM
  • User-1901014284 posted

    Hi tdinpsp,

    Thanks for your advice but when trying -30 for instead of 30 no results are displayed. When leaving as 30 I get the correct results displayed.

    Tuesday, October 2, 2018 2:11 PM
  • User475983607 posted

    Use DATEDIFF 

    SELECT *
    FROM TableName
    WHERE DATEDIFF(d, GETDATE(), ExpiryDate) = 30

    Tuesday, October 2, 2018 3:23 PM