SQL Server LIKE 子句的问题
我确信这是一个非常简单的问题,但我一直无法弄清楚。这是我的查询:
select column1 from table1 with(nolock)
where column1 like '2011-07-0[78]%'
order by column1 desc
如您所知,我正在查询一行,查找今天或昨天的时间戳。
其中一行中的完整时间戳如下所示:
2011-07-08 12:16:39.553
我已经做过很多次类似的事情,没有遇到任何问题,但无论我尝试什么,上面的查询都不会返回任何结果。 (是的,该列中有今天和昨天的时间戳)。
我犯了语法错误吗?我疯了吗?我的数据库中是否有侏儒扰乱了我的查询?请帮忙!太感谢了!
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:16: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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果该字段的数据类型为
datetime
或smalldatetime
,则LIKE
将无法按预期工作。您可以执行
DATEPART
函数或BETWEEN
,例如:或
WHERE column1 BETWEEN '2011-07-07' AND '2011-07-08'
请记住
BETWEEN
是包容性的。If the datatype for that field is
datetime
orsmalldatetime
thenLIKE
won't work as expected.You could do a
DATEPART
function orBETWEEN
, like:or
WHERE column1 BETWEEN '2011-07-07' AND '2011-07-08'
Bear in mind
BETWEEN
is inclusive.试试这个:
try this :