如何比较SQL Server 2005中的两个日期时间值?

时间:2023-02-08 12:01:52

I need to compare two datetime values in SQL Server 2005 as below.

我需要比较SQL Server 2005中的两个日期时间值,如下所示。

 case when min(@datetime1) < max(@datetime2) then 0 else 1 end

I tried with the above condition even converting the datetime datatype into varchar. But I am not able to execute it. Can anybody help me?

我尝试使用上述条件甚至将datetime数据类型转换为varchar。但我无法执行它。有谁能够帮助我?

Thanks in advance

提前致谢

3 个解决方案

#1


2  

You seem to have extra and unnecessary parenthesis around your DateTime variables when comparing - try this:

在比较时,你似乎在DateTime变量周围有额外的和不必要的括号 - 试试这个:

declare @datetime1 datetime 
set @datetime1 = '2008-09-03 10:42:46.000' 

declare @datetime2 datetime 
set @datetime2 = '2009-01-20 19:26:16.053' 

select 
    case when @datetime1 < @datetime2 then 0 else 1 end

Works just fine for me, and return the value 0 when executed.

对我来说工作正常,并在执行时返回值0。

Also, you don't need any MIN() or MAX() functions or anything like that at all - just compare the two variables and that's all there really is!

此外,您根本不需要任何MIN()或MAX()函数或类似的东西 - 只需比较两个变量就可以了!

#2


0  

You probably have some null values in CREATED_ON column.

您可能在CREATED_ON列中有一些空值。

#3


0  

i tried same query then
i got answer and this query is

我尝试了相同的查询然后我得到了答案,这个查询是

" SELECT a=CASE WHEN min(OrderDate) < max(OrderDate) THEN 0 ELSE 1 END FROM Orders WHERE OrderID = 10248 AND EmployeeID = 5 GROUP BY OrderDate " 



and this is working correctly..

这是正常的..

#1


2  

You seem to have extra and unnecessary parenthesis around your DateTime variables when comparing - try this:

在比较时,你似乎在DateTime变量周围有额外的和不必要的括号 - 试试这个:

declare @datetime1 datetime 
set @datetime1 = '2008-09-03 10:42:46.000' 

declare @datetime2 datetime 
set @datetime2 = '2009-01-20 19:26:16.053' 

select 
    case when @datetime1 < @datetime2 then 0 else 1 end

Works just fine for me, and return the value 0 when executed.

对我来说工作正常,并在执行时返回值0。

Also, you don't need any MIN() or MAX() functions or anything like that at all - just compare the two variables and that's all there really is!

此外,您根本不需要任何MIN()或MAX()函数或类似的东西 - 只需比较两个变量就可以了!

#2


0  

You probably have some null values in CREATED_ON column.

您可能在CREATED_ON列中有一些空值。

#3


0  

i tried same query then
i got answer and this query is

我尝试了相同的查询然后我得到了答案,这个查询是

" SELECT a=CASE WHEN min(OrderDate) < max(OrderDate) THEN 0 ELSE 1 END FROM Orders WHERE OrderID = 10248 AND EmployeeID = 5 GROUP BY OrderDate " 



and this is working correctly..

这是正常的..