Jens Wagemaker
Jens Wagemaker

Reputation: 374

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

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?

Upvotes: 0

Views: 158

Answers (1)

Martin Smith
Martin Smith

Reputation: 453707

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)

Upvotes: 2

Related Questions