MS SQL比较日期?


86

我有2个日期(日期时间):

date1 = 2010-12-31 15:13:48.593
date2 = 2010-12-31 00:00:00.000

在同一天,只是时间不同。由于日期1的时间,使用<=比较日期1和日期2不起作用。因此date1 <= date2是错误的,但应为true。我可以仅通过查看年,月和日来比较它们是否相同?它的SQL Server 2008。

谢谢 :)


什么版本的SQL Server?您在什么情况下进行此比较(如果要与列进行比较,则需要小心以使事物保持可查询状态)?
马丁·史密斯

我正在“选择案例”中执行此操作。如果日期是<=,则执行此操作;否则,请执行此操作。它的SQL Server 2008
grady

为了使比较失败,您的日期似乎存储为字符串。如果将它们存储为日期时间,我猜是比较
帕斯卡

date1 <= date2 = true?3PM小于12AM是怎么回事?
4

Answers:


88
SELECT CASE WHEN CAST(date1 AS DATE) <= CAST(date2 AS DATE) ...

应该做你所需要的。

测试用例

WITH dates(date1, date2, date3, date4)
     AS (SELECT CAST('20101231 15:13:48.593' AS DATETIME),
                CAST('20101231 00:00:00.000' AS DATETIME),
                CAST('20101231 15:13:48.593' AS DATETIME),
                CAST('20101231 00:00:00.000' AS DATETIME))
SELECT CASE
         WHEN CAST(date1 AS DATE) <= CAST(date2 AS DATE) THEN 'Y'
         ELSE 'N'
       END AS COMPARISON_WITH_CAST,
       CASE
         WHEN date3 <= date4 THEN 'Y'
         ELSE 'N'
       END AS COMPARISON_WITHOUT_CAST
FROM   dates 

退货

COMPARISON_WITH_CAST   |  COMPARISON_WITHOUT_CAST
Y                         N

1
请注意,DATE类型是不可用的SQL Server 2008之前
LukeH

@Luke-是的 因此,我要查询OP的版本。
马丁·史密斯

@grady-是的!查看测试用例,它确实满足您的要求!
马丁·史密斯

68

使用DATEDIFF日期部分为的函数day

SELECT ...
FROM ...
WHERE DATEDIFF(day, date1, date2) >= 0

请注意,如果要测试date1<=,date2则需要测试DATEDIFF(day, date1, date2) >= 0,否则可以测试DATEDIFF(day, date2, date1) <= 0


这行得通,但是您能解释为什么吗?这只是比较天数吗?
grady

1
@grady:否,它计算和之间的天界限。也就是说,午夜的数量你穿过从中获取到date1date2date1date2
LukeH

5
+1最优雅的解决方案。我会考虑使用ABS或<> 0,但如果需要在date1之前允许date2
gbn

3

简单的一线解决方案是

datediff(dd,'2010-12-31 15:13:48.593','2010-12-31 00:00:00.000')=0

datediff(dd,'2010-12-31 15:13:48.593','2010-12-31 00:00:00.000')<=1

datediff(dd,'2010-12-31 15:13:48.593','2010-12-31 00:00:00.000')>=1

您可以尝试使用除“ dd”以外的其他选项


0

试试这个:

BEGIN

declare @Date1 datetime
declare @Date2 datetime

declare @chkYear int
declare @chkMonth int
declare @chkDay int
declare @chkHour int
declare @chkMinute int
declare @chkSecond int
declare @chkMiliSecond int

set @Date1='2010-12-31 15:13:48.593'
set @Date2='2010-12-31 00:00:00.000'

set @chkYear=datediff(yyyy,@Date1,@Date2)
set @chkMonth=datediff(mm,@Date1,@Date2)
set @chkDay=datediff(dd,@Date1,@Date2)
set @chkHour=datediff(hh,@Date1,@Date2)
set @chkMinute=datediff(mi,@Date1,@Date2)
set @chkSecond=datediff(ss,@Date1,@Date2)
set @chkMiliSecond=datediff(ms,@Date1,@Date2)

if @chkYear=0 AND @chkMonth=0 AND @chkDay=0 AND @chkHour=0 AND @chkMinute=0 AND @chkSecond=0 AND @chkMiliSecond=0
    Begin
        Print 'Both Date is Same'
    end
else
    Begin
        Print 'Both Date is not Same'
    end
End

0

我总是用DateDiff(day,date1,date2)比较两个日期。

签出以下示例。只需将其复制并在Ms sql服务器中运行即可。另外,尝试将更改日期从12月31日更改为12月30日,然后检查结果

BEGIN

declare @firstDate datetime
declare @secondDate datetime


declare @chkDay int

set @firstDate ='2010-12-31 15:13:48.593'
set @secondDate ='2010-12-31 00:00:00.000'

set @chkDay=Datediff(day,@firstDate ,@secondDate )

if @chkDay=0
    Begin
        Print 'Date is Same'
    end
else
    Begin
        Print 'Date is not Same'
    end
End
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.