我正在为特定的时间范围创建TSQL参数,例如MorningTimeFrame将从昨天下午4点拉到今天中午,而AfternoonTimeFrame从今天下午拉到今天4Pm。我该如何设置?我熟悉Dateadd(day,datediff(day,1,GETDATE()),0),但不确定如何将其设置为特定时间。
答案 0 :(得分:0)
您可以根据以下查询获取这些所需的时间范围值
Select Cast(Cast(CAST(dateadd(d,-1,getdate()) AS date) as varchar) + ' 16:00:00' as datetime)AS 'MorningStartTime', Cast(Cast(CAST(getdate() AS date) as varchar) + ' 12:00:00' as datetime)AS 'MorningEndAfterNoonStartTime', Cast(Cast(CAST(getdate() AS date) as varchar) + ' 16:00:00' as datetime)AS 'AfterNoonEndTime'
为方便起见使用此
答案 1 :(得分:0)
简单一些就够了吗?
declare @Start as DateTime, @End as DateTime;
declare @Timeframe as VarChar(32) = 'MorningTimeframe';
-- Start from midnight today.
declare @Today as DateTime = Cast( GetDate() as Date );
-- Calculate the timeframe.
if @Timeframe = 'MorningTimeframe'
begin -- Yesterday 4PM through noon today.
select @Start = DateAdd( hour, -8, @Today ),
@End = DateAdd( hour, 12, @Today );
end
else if @Timeframe = 'AfternoonTimeframe'
begin -- Noon today through 4PM.
select @Start = DateAdd( hour, 12, @Today ),
@End = DateAdd( hour, 16, @Today );
end
else
begin
RaIsError( 'Unexpected timeframe: ''%s''', 16, 42, @Timeframe );
end;
-- Display the results.
select @Today as [Today], @Timeframe as Timeframe, @Start as [Start], @End as [End];