SQL的日期条件

编程入门 行业动态 更新时间:2024-10-28 18:33:35
本文介绍了SQL的日期条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好,SQL Server 2008 R2 今天是星期五,我的大脑受伤了.我想查找昨天的所有记录,所以我有

Hello, SQL Server 2008 R2 It is Friday and my brain hurts. I want to find all records for yesterday so I have

SELECT * FROM myTable WHERE myDateTime = ''1/27/2011''

由于记录有时间,因此不会这样做.因此,我想做的事情类似

This will not do it as the records have the time. Thus what I would like is to do something like

WHERE CAST(myDateTime AS DATE) = ''1/27/2011''

,但这会破坏索引. 所以我需要使用类似

however this kills indexing. So do I need to use something like

WHERE myDateTime BETWEEN ''1/27/2011'' AND ''1/28/2011''

的东西吗 谢谢 djj

Thank you, djj

推荐答案

WHERE myDateTime >= ''1/27/2011'' and myDateTime < ''1/28/2011''

*考虑我们的讨论

*considering our discussion

WHERE (CONVERT(nvarchar, myDateTime, 101) = ''01/27/2011'')

如果您想使用单个日期进行操作,则可以执行以下操作 If you wanted to do it using a single date, you could do something like this select * FROM SomeTable WHERE CAST(FLOOR( CAST( YourField AS FLOAT ) )AS DATETIME) = '1/27/2011'

因此,从字段中删除时间,然后与某个值进行比较 更新:抱歉,我看到了您关于该杀害指标的帖子 否则,唯一的方法是使用BETWEEN.

So, strip out the time from the field and then compare to some value Update: sorry, I saw your post about that killing indexes Otherwise, the only way to do it is to use a BETWEEN.

DECLARE @StartDate DATETIME DECLARE @EndDate DATETIME SET @StartDate = CAST(FLOOR( CAST( GETDATE-1 AS FLOAT ) )AS DATETIME) SET @EndDate = CAST(FLOOR( CAST( GETDATE AS FLOAT ) )AS DATETIME) select * FROM SomeTable WHERE YourField BETWEEN @StartDate AND @EndDate

有关: WHERE myDateTime >= ''1/27/2011''

更多推荐

SQL的日期条件

本文发布于:2023-10-24 18:16:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   日期   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!