我通过以下查询获取当前系统时间
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部分改为“从表中选择时间”
由于
答案 0 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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
答案 3 :(得分:0)
你可以得到这样的时间:
select replace(cast(cast(getdate() as time) as varchar(5)), ':', '')
它从当时获得前5个字符,然后删除:。
答案 4 :(得分:0)
以这种格式获得时间是
select REPLACE( CONVERT(VARCHAR(5),CURRENT_TIMESTAMP,108), ':', '')