在 SQL Server 中比较没有时间的日期的最佳方法

发布于 2025-01-06 07:52:01 字数 319 浏览 3 评论 0原文

select * from sampleTable 
where CONVERT(VARCHAR(20),DateCreated,101) 
=     CONVERT(VARCHAR(20),CAST('Feb 15 2012  7:00:00:000PM' AS DATETIME),101)

我想比较没有时间的日期

上述查询可以吗?或您建议的其他更好的解决方案

  • 我正在使用 SQL Server 2005
  • 在服务器上以 UTC 格式保存的日期
  • 用户针对此数据属于不同的时区
select * from sampleTable 
where CONVERT(VARCHAR(20),DateCreated,101) 
=     CONVERT(VARCHAR(20),CAST('Feb 15 2012  7:00:00:000PM' AS DATETIME),101)

I want to compare date without time

Is above query is ok? or other better solution you suggest

  • I am using SQL Server 2005
  • Date saved in UTC format on server
  • Users against this data belongs different timezone

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

几味少女 2025-01-13 07:52:01

简单地转换为日期即可解决该问题。

DECLARE @Date datetime = '04/01/2016 12:01:31'

DECLARE @Date2 datetime = '04/01/2016'

SELECT CAST(@Date as date)

SELECT CASE When (CAST(@Date as date) = CAST(@Date2 as date)) Then 1 Else 0 End

Simple Cast to Date will resolve the problem.

DECLARE @Date datetime = '04/01/2016 12:01:31'

DECLARE @Date2 datetime = '04/01/2016'

SELECT CAST(@Date as date)

SELECT CASE When (CAST(@Date as date) = CAST(@Date2 as date)) Then 1 Else 0 End
贱贱哒 2025-01-13 07:52:01

不要使用转换 - 无缘无故地涉及字符串。一个技巧是,日期时间实际上是一个数字,天数是整数部分(时间是小数部分);因此,日期是值的FLOOR:这只是数学,而不是字符串 - 快得多

declare @when datetime = GETUTCDATE()
select @when -- date + time
declare @day datetime = CAST(FLOOR(CAST(@when as float)) as datetime)
select @day -- date only

在您的情况下,无需转换回日期时间;并且使用范围可以进行最有效的比较(特别是如果有索引):

declare @when datetime = 'Feb 15 2012  7:00:00:000PM'
declare @min datetime = FLOOR(CAST(@when as float))
declare @max datetime = DATEADD(day, 1, @min)

select * from sampleTable where DateCreated >= @min and DateCreated < @max

Don't use convert - that involves strings for no reason. A trick is that a datetime is actually a numeric, and the days is the integer part (time is the decimal fraction); hence the day is the FLOOR of the value: this is then just math, not strings - much faster

declare @when datetime = GETUTCDATE()
select @when -- date + time
declare @day datetime = CAST(FLOOR(CAST(@when as float)) as datetime)
select @day -- date only

In your case, no need to convert back to datetime; and using a range allows the most efficent comparisons (especially if indexed):

declare @when datetime = 'Feb 15 2012  7:00:00:000PM'
declare @min datetime = FLOOR(CAST(@when as float))
declare @max datetime = DATEADD(day, 1, @min)

select * from sampleTable where DateCreated >= @min and DateCreated < @max
坐在坟头思考人生 2025-01-13 07:52:01
SELECT .......
FROM ........
WHERE 
CAST(@DATETIMEVALUE1 as DATE) = CAST(@DATETIMEVALUE2 as DATE)

缺点是您要铸造过滤柱。

如果过滤列上有索引,那么,由于您正在进行转换,SQL 引擎无法再使用索引来更有效地过滤日期。

SELECT .......
FROM ........
WHERE 
CAST(@DATETIMEVALUE1 as DATE) = CAST(@DATETIMEVALUE2 as DATE)

The disadvantage is that you are casting the filter column.

If there is an index on the filter column, then, since you are casting, the SQL engine can no longer use indexes to filter the date more efficiently.

蓝眸 2025-01-13 07:52:01

描述

不要将 Date 转换为 varchar 并进行比较,因为字符串比较速度不快。

如果您使用 >=< 来过滤 DateCreated 列,速度会快得多。

如果您没有参数(如示例中的字符串),则应使用 ISO 格式

示例

根据您的示例

DECLARE @startDate DateTime
DECLARE @endDate DateTime

SET @startDate = '20120215'
SET @endDate = DATEADD(d,1,@startDate)

SELECT * FROM sampleTable 
WHERE DateCreated >= @startDate AND DateCreated < @endDate

更多信息

Description

Don't convert your Date to a varchar and compare because string comparisson is not fast.

It is much faster if you use >= and < to filter your DateCreated column.

If you have no parameter (like in your sample, a string) you should use the ISO Format <Year><Month><Day>.

Sample

According to your sample

DECLARE @startDate DateTime
DECLARE @endDate DateTime

SET @startDate = '20120215'
SET @endDate = DATEADD(d,1,@startDate)

SELECT * FROM sampleTable 
WHERE DateCreated >= @startDate AND DateCreated < @endDate

More Information

Hello爱情风 2025-01-13 07:52:01

请使用 112 CONVERT 格式

select * 
from sampleTable 
where 
  CONVERT(VARCHAR(20),DateCreated,112) 
=     CONVERT(VARCHAR(20),CAST('Feb 15 2012  7:00:00:000PM' AS DATETIME),112)

如果您的 sql server 版本 2008+ 使用 DATE 类型,

select * from sampleTable 
where CONVERT(DATE,DateCreated) 
=     CONVERT(DATE,CAST('Feb 15 2012  7:00:00:000PM' AS DATETIME))

Use 112 CONVERT's format

select * 
from sampleTable 
where 
  CONVERT(VARCHAR(20),DateCreated,112) 
=     CONVERT(VARCHAR(20),CAST('Feb 15 2012  7:00:00:000PM' AS DATETIME),112)

or

if your sql server version 2008+ use DATE type

select * from sampleTable 
where CONVERT(DATE,DateCreated) 
=     CONVERT(DATE,CAST('Feb 15 2012  7:00:00:000PM' AS DATETIME))
浅浅 2025-01-13 07:52:01

声明 @DateToday Date= '2019-10-1';
打印@DateToday;

print Abs(datediff(day, @DateToday,CAST('2019 年 10 月 1 日 7:00:00:000PM' AS DATETIME))) < 3

这是3天后的比较。

我在 SQL Server 2014 上测试了这个,它有效。

declare @DateToday Date= '2019-10-1';
print @DateToday;

print Abs(datediff(day, @DateToday,CAST('oct 1 2019 7:00:00:000PM' AS DATETIME))) < 3

this is compare whin 3 days.

i test this on SQL Server 2014, it works.

秋意浓 2025-01-13 07:52:01
select * from sampleTable 
where date_created ='20120215'

这还将您的列与特定日期进行比较
不考虑时间

select * from sampleTable 
where date_created ='20120215'

This will also compare your column with the particular date
without taking time into account

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