如果getdate()已经过了一段时间,则显示一个字符串

时间:2015-01-08 14:26:43

标签: sql sql-server tsql time getdate

所以这就是我所拥有的:

declare @breakend datetime,
        @breakstart datetime
set @breakend = (select Convert(varchar(20),DateAdd(month, 0, convert(varchar(20),getdate(), 102) + Space(1) + '9:00:00 AM'), 120))
set @breakstart = GETDATE()
select CONVERT(VARCHAR(20),DateAdd(SS,Datediff(ss,@breakstart, @breakend)%(60*60*24),0),108) as 'Hours until first break'

如果时间过了上午9点,我想知道如何打印“过去休息时间”这样的东西?

所以如果它早上8点它会显示'休息前1小时'。 如果它在上午9:01显示'过去休息时间'

3 个答案:

答案 0 :(得分:1)

您可以使用casebetween。如果我理解正确的话,你可以检查你的日期是否在休息时间之间。

declare @breakend datetime,
        @breakstart datetime
set @breakend = getdate() + 1
set @breakstart = GETDATE() - 1
select case when getdate() between @breakstart and @breakend then 1 else 0 end as 'result'

答案 1 :(得分:0)

这不是确切的小时数,但可能会有效

declare @breakend datetime,
    @breakstart datetime
set @breakend = (select cast(cast(getdate() as date) as varchar) + ' 09:00:00')
set @breakstart = getdate()

select
case 
    when @breakstart < @breakend then cast(datediff(hour,@breakstart,@breakend) as varchar) + ' hours until break'
    else 'Past break time'
end display

答案 2 :(得分:0)

DECLARE @breakend datetime,
        @breakstart datetime
SET @breakend = (select Convert(varchar(20),DateAdd(month, 0, convert(varchar(20),getdate(), 102) + Space(1) + '09:00:00 AM'), 120))
SET @breakstart = GETDATE()

SELECT
    CASE WHEN @breakstart < @breakend THEN CAST(DATEDIFF(MINUTE,@breakstart,@breakend) as varchar(10)) + ' minutes left on break'
            ELSE 'Past Break Time' END AS Value