views:

65

answers:

5

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.

A: 

Strip the time if that is irrelevant

Dateadd(d, datediff(d,0,getdate()),0)

JonH
I need the time unfortunately
Isawpalmetto
A: 

When I try running something similar in my version of SQL Server (2005), it works as expected.

Try taking out the PM and using 24 hour format instead:

SELECT * 
FROM Res 
Where dateSubmit='6/17/2010 17:01:26' 
froadie
I tried that before and just tried again, but still doesn't work.
Isawpalmetto
A: 

That looks like it should work. You might try SELECT * FROM Res Where cast(dateSubmit as nvarchar) ='6/17/2010 5:01:26PM'

MikeTWebb
this will negate index usage, it is better to cast the string to a datetime, that way an index can be used. Also, CONVERT is better for taking `datetime` to string, you can specify a format.
KM
Also - when I try this it doesn't match, as the cast as nvarchar returns the month as a string as well - i.e. "jun"
froadie
Yeah, convert would be better
MikeTWebb
+6  A: 

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'
Mark Ransom
Yup, that did the trick. Thanks a lot. It must be keeping track of the milliseconds as well or something.
Isawpalmetto
- nice catch :)
froadie
you really want `dateSubmit>='6/17/2010 5:01:26 PM' and dateSubmit<'6/17/2010 5:01:27 PM'`, because `BETWEEN` will give you rows `<=` the end range value, and you just want `<` the end value.
KM
In my experience, SQL Server behaves better if you use ISO standard datetime strings: `'2010-06-17T17:01:26'` If you're using a parameter, be sure to strip the milliseconds before attempting this.
Toby
@KM, very good point. I had thought that the end of the range was excluded, but I just checked the docs for BETWEEN and you are correct.
Mark Ransom
@Toby, I prefer ISO format too, it's unambiguous. I didn't think it was relevant to the question though.
Mark Ransom
SQL Server's datetime does indeed keep track of milliseconds, aka fractional seconds, up until three positions.The details: http://msdn.microsoft.com/en-us/library/ms187819.aspx
Valentino Vranken
@Mark Ransom, that is why I **never** use `BETWEEN`, it is more clear to just use `>=` or `>`, and `<=` or `<`.
KM
+1  A: 

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
Waleed A.K.