0

我通过以下查询获取当前系统时间

select cast(DATEPART(hh,getdate()) as varchar)+ ':'+ cast(DATEPART(n,getdate()) as varchar)

现在如何将此当前系统时间与时间字段进行比较以查看哪个更大?

 -----------------------
 declare    @time1  datetime,
            @time2  datetime

 select  @time1 = '20060701 02:27:35.35',
         @time2 = getdate()

 select tm1, tm2,
        case    
         when tm1 = tm2 then 'tm1 = tm2' 
         when tm1 > tm2 then 'tm1 > tm2'
    else        'tm1 < tm2'
    end as [TimeDiff]
 from
 (
     select dateadd(day, -datediff(day, 0, @time1), @time1) as tm1,
            dateadd(day, -datediff(day, 0, @time2), @time2) as tm2
 ) t

 -----------------------

时间字段具有以下格式的时间

     0400
     0415
     0430

我看到了这个支持功能。现在如何将@time1 部分​​更改为“从表中选择时间”

谢谢

4

5 回答 5

1

试试这个:

select case when cast(cast(DATEPART(hh,getdate()) as varchar)+ ':'+ cast(DATEPART(n,getdate()) as varchar) as datetime) >
       cast(left('0415',2)+':'+right('0415',2) as datetime) then 'greter' else 'lesser'
       end
于 2012-11-23T12:06:46.027 回答
1

如果您只想将@time1 替换为表格列。在这种情况下,给你,

declare    @time2  datetime

CREATE Table #temp
(
time1 varchar(100)

)
Insert into #temp 
 select '20060701 02:27:35.35'

Insert into #temp 
 select '20060701 12:27:35.35'

 Insert into #temp 
 select '20060701 22:27:35.35'

 Select   @time2 = getdate()

 select tm1, tm2,
        case    
         when tm1 = tm2 then 'tm1 = tm2' 
         when tm1 > tm2 then 'tm1 > tm2'
         else        'tm1 < tm2'
        end as [TimeDiff]
 from
 (
     select dateadd(day, -datediff(day, 0, time1), time1) as tm1,
            dateadd(day, -datediff(day, 0, @time2), @time2) as tm2

        FROM #temp
 ) t
于 2012-11-23T12:38:01.437 回答
0
Declare @time varchar(10)
SET @time='0400'
select CASE when convert(datetime,left(@time,2)+':'+RIGHT(@time,2))>convert(datetime,cast(DATEPART(hh,getdate()) as varchar)+ ':'+ cast(DATEPART(n,getdate()) as varchar)) then '1' else '0' end
于 2012-11-23T12:02:39.120 回答
0

你可以像这样得到时间:

select replace(cast(cast(getdate() as time) as varchar(5)), ':', '')

它从 time 中获取前 5 个字符,然后删除 :。

于 2012-11-23T12:09:07.007 回答
0

以这种格式获取时间是

select REPLACE( CONVERT(VARCHAR(5),CURRENT_TIMESTAMP,108), ':', '')
于 2012-11-23T14:15:15.680 回答