Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare a datetime type in SQL Server

So I have a column in my DB on SQL Server that is a datetime. I want to do this simple select statement:

SELECT *
FROM Res
Where dateSubmit='6/17/2010 5:01:26 PM'

dateSubmit is a datetime data type and my database has a row where dateSubmit is 6/17/2010 5:01:26 PM

However, when I execute this statement it returns null.

like image 708
Isawpalmetto Avatar asked Jun 30 '26 09:06

Isawpalmetto


2 Answers

I think SQL Server keeps more digits of precision than it is displaying.

Try this:

SELECT * 
FROM Res 
Where dateSubmit between '6/17/2010 5:01:26 PM' and '6/17/2010 5:01:27 PM'
like image 102
Mark Ransom Avatar answered Jul 02 '26 09:07

Mark Ransom


The problem is the hour: minute: sec part and you will never get exact sec that’s why you’ll get null. Try to use

SELECT * 
FROM Res
Where DATEDIFF ( day , dateSubmit , '6/17/2010 5:01:26 PM' )=0

For more information look to this link

The “day” could be replace by anything else as “DatePart” see the link e.g.

SELECT * 
FROM Res
Where DATEDIFF ( minute, dateSubmit , '6/17/2010 5:01:26 PM' )=0
like image 39
Waleed A.K. Avatar answered Jul 02 '26 10:07

Waleed A.K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!