Issues with SQL Server LIKE clause
I'm sure this is a VERY simple problem, but I've not been able to figure it out. Here's my query:
select column1 from table1 with(nolock)
where column1 like '2011-07-0[78]%'
order by column1 desc
As you can tell, I'm querying a row looking for timestamps that are from either today or yesterday.
A full timestamp within one of these rows looks like this:
2011-07-08 12:开发者_如何学JAVA16:39.553
I've done similar things many times with no trouble, but the above query returns no results no matter what I try. (Yes, there are timestamps from today and yesterday in the column).
Have I made a syntax error? Am I crazy? Are there gnomes in my DB messing with my query? Please help! Thank you so much!
If the datatype for that field is datetime
or smalldatetime
then LIKE
won't work as expected.
You could do a DATEPART
function or BETWEEN
, like:
WHERE DATEPART(Year, column1) = 2011
AND DATEPART(Month, column1) = 7
AND DATEPART(Day, column1) IN (7, 8)
or
WHERE column1 BETWEEN '2011-07-07' AND '2011-07-08'
Bear in mind BETWEEN
is inclusive.
try this :
select column1 from table1 with(nolock)
where Cast(column1 as DateTime) between '2011-07-07' AND '2011-07-08'
order by column1 desc
精彩评论