Thursday, 24 November 2011

mssql - compare two datetime fields


problem

mssql - compare two datetime
Mssql, compare two datetime fields, if their values are close, if are in 24h difference and do on
---
> dennis
difficulty level

1/10 :)))
compatibility

mssql
solution

In mssql, the datetime fields are actually numeric with decimals. The integer part in the “days”. So if you want to compare two dates compare the integer part as bellow

-- compare if the item is sold 24h before the item is modified
Select * from items
Where ModificationDate<SaleDate-1

-- find person(s) that born on 12/11/2010 (not exactly on the same, with at least 12 hours difference)
Select * from persons
Where birthdate-0.5>=’2010-12-11’ and birthdate+0.5<=’2010-12-11’

No comments:

Post a Comment