Compare DATETIME and DATE ignoring time portion

abatishchev picture abatishchev · Dec 3, 2009 · Viewed 254.5k times · Source

I have two tables where column [date] is type of DATETIME2(0).

I have to compare two records only by theirs Date parts (day+month+year), discarding Time parts (hours+minutes+seconds).

How can I do that?

Answer

marc_s picture marc_s · Dec 3, 2009

Use the CAST to the new DATE data type in SQL Server 2008 to compare just the date portion:

IF CAST(DateField1 AS DATE) = CAST(DateField2 AS DATE)