Home > Net >  What happens when you do a comparison "date <= datetime" in SQL-server?
What happens when you do a comparison "date <= datetime" in SQL-server?

Time:09-15

What happens when you do a comparison "date <= datetime" in SQL-server?

Are they both cast to date or are they both cast to datetime in order to do the comparison?

CodePudding user response:

datetime has higher precedence than date so the date will be cast to datetime.

This is documented here.

Or you can also see this below

DECLARE @D DATETIME = '2022-09-15 11:59:59';

SELECT CASE WHEN @D = CAST(@D AS DATE) THEN 'Equal' ELSE 'NotEqual' END 

(Returns "NotEqual". If the implicit casting was to date and the time portion removed both would be equal)

  • Related