How can I compare two datetime values in SQL Server 2005?
I need to compare two datetime
values in SQL Server 2005 as below.
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?
Thanks in advance
You seem to have extra and unnecessary parenthesis around your DateTime
variables when comparing - try this:
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.
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!
You probably have some null values in CREATED_ON column.
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..
精彩评论