Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Can Datime be queried on by date format?

Author  Topic 

mgreen84
Yak Posting Veteran

94 Posts

Posted - 2009-09-15 : 14:29:26
Can a Datime formatted field be queried on by just using the yyyy-mm-dd format?

example: would Where date >='2009-08-04'
bring back '2009-08-04 11:46:38.533' in the results?

If not why?

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-09-15 : 14:38:37
Yes it would. The main thing to try to avoid is converting the datetime column values from datetime. So your solution is superior to:
where convert(varchar, [dateColumn], 101) = '2009-08-04'

Be One with the Optimizer
TG
Go to Top of Page

mgreen84
Yak Posting Veteran

94 Posts

Posted - 2009-09-15 : 14:46:10
k, I ask because Im doing the following, and I pick up more records with this than the method further down

a.historydate >='2009-08-01 00:00:00.000'
a.historydate <='2009-08-31 23:59:59:997'



a.historydate >='2009-08-01'
and a.historydate <='2009-08-31'
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-09-15 : 15:01:24
quote:
Originally posted by mgreen84

k, I ask because Im doing the following, and I pick up more records with this than the method further down

a.historydate >='2009-08-01 00:00:00.000'
a.historydate <='2009-08-31 23:59:59:997'



a.historydate >='2009-08-01'
and a.historydate <='2009-08-31'



It would be better to do it this way:
a.historydate >= '2009-08-01' 
and a.historydate < '2009-09-01'





CODO ERGO SUM
Go to Top of Page

mgreen84
Yak Posting Veteran

94 Posts

Posted - 2009-09-15 : 15:11:35
i will, but do you know why I would get more results one way rather than the other?
Go to Top of Page

mgreen84
Yak Posting Veteran

94 Posts

Posted - 2009-09-15 : 15:29:32
like why wouldn't

a.historydate >='2009-08-01 00:00:00.000'
a.historydate <='2009-08-31 23:59:59:997'


give me the same results as

a.historydate >='2009-08-01'
a.historydate <='2009-08-31'

?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-15 : 21:18:55
quote:
Originally posted by mgreen84

like why wouldn't

a.historydate >='2009-08-01 00:00:00.000'
a.historydate <='2009-08-31 23:59:59:997'


give me the same results as

a.historydate >='2009-08-01'
a.historydate <='2009-08-31'

?


No. You will exclude any records with historydate more than '2009-08-31' like '2009-08-31 00:00:01' in the second condition


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -