Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading