Home > Mobile >  Why is the GETDATE() function not working in IF condition?
Why is the GETDATE() function not working in IF condition?

Time:10-06

I have created 1 code which should run only once in a day around 4 pm everyday.

The below condition I am running but it's not returning the desired output.

DECLARE @to_date AS date 

SET @to_date = GETDATE()

IF @to_date = GETDATE()
BEGIN
    INSERT INTO Tem1
        SELECT * FROM Temp2
END

CodePudding user response:

GetDate() is not deterministic. This means it can (and most probably does) give you a different value in each call. Using equals with getdate() will almost always be wrong. Try less than @to_date instead.

CodePudding user response:

Because of precedence rules - date is implicitly converted to datetime for comparison. You expect the opposite.

  • Related