calculate dates

  • I need to compare dates
    example declare @date date = '6/18/2018'

    I need to compare @date should be less than getdate -3

    SELECT '6/18/2018'< (Getdate()- 3)  --- how do we write this?

  • komal145 - Thursday, June 21, 2018 11:02 AM

    I need to compare dates
    example declare @date date = '6/18/2018'

    I need to compare @date should be less than getdate -3

    SELECT '6/18/2018'< (Getdate()- 3)  --- how do we write this?

    Either in a WHERE clause
    SELECT * FROM Table
    WHERE '20180618' < (Getdate()- 3) 

    or a CASE expression.
    SELECT CASE WHEN '20180618'< (Getdate()- 3) THEN 'True' ELSE 'False' END
    FROM Table

    Note that  you still need to handle time and that I changed your date into an international format.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Not sure what you are trying to accomplish here.  If you are trying to compare a date on the table to be < (Getdate()- 3) then use Luis's first example, replacing the '20180618' with the column from the table.
    DECLARE @date DATE = '6/18/2018';

    SELECT CASE    WHEN @date < (Getdate()- 3)
                    THEN 'True'
                ELSE
                    'False'
        END As GoodDateFlag
    FROM Table
    ;

    -------------------------------------------------------------
    we travel not to escape life but for life not to escape us
    Don't fear failure, fear regret.

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

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