I have two dates (datetimes):
date1 = 2010-12-31 15:13:48.593
date2 = 2010-12-31 00:00:00.000
It's the same day, just different times. Comparing date1 and date2 using <= doesn’t work because of the date1 time. So date1 <= date2 is wrong, but it should be true. Can I compare them by just looking at the year, month and day so they are the same? It's SQL Server 2008.
6条答案
按热度按时间50pmv0ei1#
Should do what you need.
Test Case
Returns
gg0vcinb2#
Use the
DATEDIFF
function with a datepart ofday
.Note that if you want to test that
date1
<=date2
then you need to test thatDATEDIFF(day, date1, date2) >= 0
, or alternatively you could testDATEDIFF(day, date2, date1) <= 0
.n3schb8v3#
The simple one line solution is
You can try various option with this other than "dd"
vdgimpew4#
I am always used DateDiff(day,date1,date2) to compare two date.
Checkout following example. Just copy that and run in Ms sql server. Also, try with change date by 31 dec to 30 dec and check result
brc7rcf05#
Try This:
xwmevbvl6#
I use the below script in my SQL and it is working fine with me: