SQL Server LIKE 子句的问题

发布于 2024-11-19 12:44:44 字数 410 浏览 2 评论 0原文

我确信这是一个非常简单的问题,但我一直无法弄清楚。这是我的查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱々樱花落 2024-11-26 12:44:44

如果该字段的数据类型为 datetimesmalldatetime,则 LIKE 将无法按预期工作。

您可以执行 DATEPART 函数或 BETWEEN,例如:

WHERE DATEPART(Year, column1) = 2011
AND DATEPART(Month, column1) = 7
AND DATEPART(Day, column1) IN (7, 8)

WHERE column1 BETWEEN '2011-07-07' AND '2011-07-08'

请记住 BETWEEN 是包容性的。

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.

孤君无依 2024-11-26 12:44:44

试试这个:

select column1 from table1 with(nolock)
where Cast(column1 as DateTime) between '2011-07-07' AND '2011-07-08'
order by column1 desc

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文