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)
 datetime

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-10-06 : 06:57:00
Table1 has a field called MyDate which has entries into it with date and time.
When I do a select query on table1.MyDate iin the where clause I pass in a date such as '01 sep 2010' i.e. where table1.MyDate = '01 sep 2010'
This does not return anything but there are records for '01 Sep 2010 10:20:23', etc.
How should the sql be altered to get this data please?
Thanks

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2010-10-06 : 07:01:27
I have now solved this by converting to varchar(11) and then into datetime again.
Thank you anyway.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-10-06 : 07:18:46
not a good plan. You won't be able to use any index on the column(s) using CAST/CONVERT on the column

There are many, many, many posts for exactly this question.

Best solution is generally to do a


WHERE
[col] >= '20100901' AND [col] < '20100902'


Which *can* use an index on [col] and will bring back any entry for 01/sep/2010 (regardless of the timestamp)

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-10-06 : 07:19:45
Also -- 750 ish posts and you still pass dates not using ISO standard strings?

For shame!

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -