Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LIKE query on datetime 1

Status
Not open for further replies.

TheConeHead

Programmer
Joined
Aug 14, 2002
Messages
2,106
Location
US
How can I do something like:

select * from table where dattimeField like '%1/12/2005%'

I have tried this but it does not seem to work? Is there no way to do a LIKE on a datetime field?

[conehead]
 
Code:
select * from table where 
convert(char(10),dattimeField,101) ='01/12/2005'

try this...
 
beauty - just a couple chages:

select * from table where
convert(char(10),dattimeField,101) LIKE '%01/12/2005%'

works great!

[conehead]
 
Why have the wildcard before and after the Date?

DATETIME does not have a value before the date. The only additional value would be the time at the end. But you convert the DATETIME to get rid of the time. So you are only using 10 characters of the DATETIME (01/12/2005 is 10 characters), so there CAN'T be any leading or trailing characters.

Finally, using the wildcards when they aren't needed will eliminate the use of any index on the DATETIME field.

-SQLBill

Posting advice: FAQ481-4875
 
did you try it without the like? if you didn't get everything maybe use this...
Code:
select * from table where 
convert(char(10),dattimeField,101) ='01/12/2005'
OR
convert(char(10),dattimeField,101) ='1/12/2005'

just because like statements aren't the quickest...
 
yeah - tried it without and it did not work...

[conehead]
 
And I think this is faster than all the solutions presented so far:

Code:
WHERE
   DateTimeField >='2005-01-12' AND DateTimeField < '2005-01-13'


-------------------------------------
• Every joy is beyond all others. The fruit we are eating is always the best fruit of all.
• It is waking that understands sleep and not sleep that understands waking. There is an ignorance of evil that comes from being young: there is a darker ignorance that comes from doing it, as men by sleeping lose the k
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top