Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL是两次中最短的一次_Sql Server_Sql Server 2005 - Fatal编程技术网

Sql server SQL是两次中最短的一次

Sql server SQL是两次中最短的一次,sql-server,sql-server-2005,Sql Server,Sql Server 2005,我需要的ans是-1SQL Server 2005 我将从分别显示每个步骤的详细方法开始 Declare @time1 datetime; Declare @time2 datetime; right(@time1,7) = 9:00 Am right(@time2,7) = 3:10 Pm case when @time1 > @time2 then 1 else -1 end as small 这里的想法是我们为比较设定一个共同的基准日期。在此场景中,我们使用1900-01-01

我需要的ans是-1

SQL Server 2005 我将从分别显示每个步骤的详细方法开始

Declare @time1 datetime;
Declare @time2 datetime;

right(@time1,7) = 9:00 Am
right(@time2,7) = 3:10 Pm

case when @time1 > @time2 then 1 else -1 end as small
这里的想法是我们为比较设定一个共同的基准日期。在此场景中,我们使用
1900-01-01
(即
Cast(0作为日期时间)
)作为我们的通用日期

通用日期的原因是为了满足起点日期部分不同的情况

SQL Server 2008 SQL Server 2008引入了新的
日期
时间
数据类型。我们可以简单地
Cast()
time
并比较结果

DECLARE @time1 datetime;
    SET @time1 = Current_Timestamp;
DECLARE @time2 datetime;
    SET @time2 = DateAdd(hh, -29, @time1);

; WITH dateparts AS (
  SELECT @time1 As time1
       , DatePart(hh, @time1) As hh1
       , DatePart(mi, @time1) As mi1
       , DatePart(ss, @time1) As ss1
       , DatePart(ms, @time1) As ms1
       , @time2 As time2
       , DatePart(hh, @time2) As hh2
       , DatePart(mi, @time2) As mi2
       , DatePart(ss, @time2) As ss2
       , DatePart(ms, @time2) As ms2
)
, construct_time_only AS (
  SELECT time1
       , DateAdd(hh, hh1, DateAdd(mi, mi1, DateAdd(ss, ss1, DateAdd(ms, ms1, 0)))) As timeonly1
       , time2
       , DateAdd(hh, hh2, DateAdd(mi, mi2, DateAdd(ss, ss2, DateAdd(ms, ms2, 0)))) As timeonly2
  FROM   dateparts
)
SELECT time1
     , time2
     , timeonly1
     , timeonly2
     , CASE WHEN timeonly1 > timeonly2 THEN 1 ELSE -1 END As small
FROM   construct_time_only
;

您应该将日期作为日期类型进行比较:

只要做():

您还可以使用功能,如

Declare @date1 datetime = '20131017 09:00:00'
Declare @date2 datetime = '20131017 15:00:00'

--right(@time1,7) = 9:00 Am
--right(@time2,7) = 3:10 Pm

select case when @date1 > @date2 then 1 else -1 end as small

使用sql server2005@Venkat添加了2005解决方案。感谢我的代码本身的工作
Declare @date1 datetime = '20131017 09:00:00'
Declare @date2 datetime = '20131017 15:00:00'

--right(@time1,7) = 9:00 Am
--right(@time2,7) = 3:10 Pm

select case when @date1 > @date2 then 1 else -1 end as small
select case when datediff(second, @date1, @date2) > 0 then -1 else 1 end as small