Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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中为工作日添加小时数?_Sql_Sql Server_Tsql_Datetime - Fatal编程技术网

如何在SQL中为工作日添加小时数?

如何在SQL中为工作日添加小时数?,sql,sql-server,tsql,datetime,Sql,Sql Server,Tsql,Datetime,我看到了许多在SQL中添加工作日期(工作日)的示例。但我想增加一个小时 比如,;我想增加36小时的日期,而不是在星期日,星期六 有人能帮我吗 CREATE FUNCTION AddWorkDays ( @WorkingDays As Int, @StartDate AS DateTime ) RETURNS DateTime AS BEGIN DECLARE @Count AS Int DECLARE @i As Int DECLARE

我看到了许多在SQL中添加工作日期(工作日)的示例。但我想增加一个小时

比如,;我想增加36小时的日期,而不是在星期日,星期六 有人能帮我吗

CREATE FUNCTION AddWorkDays 
(    
    @WorkingDays As Int, 
    @StartDate AS DateTime 
) 
RETURNS DateTime 
AS
BEGIN
    DECLARE @Count AS Int
    DECLARE @i As Int
    DECLARE @NewDate As DateTime 
    SET @Count = 0 
    SET @i = 0 

    WHILE (@i < @WorkingDays) --runs through the number of days to add 
    BEGIN
-- increments the count variable 
        SELECT @Count = @Count + 1 
-- increments the i variable 
        SELECT @i = @i + 1 
-- adds the count on to the StartDate and checks if this new date is a Saturday or Sunday 
-- if it is a Saturday or Sunday it enters the nested while loop and increments the count variable 
           WHILE DATEPART(weekday,DATEADD(d, @Count, @StartDate)) IN (1,7) 
            BEGIN
                SELECT @Count = @Count + 1 
            END
    END

-- adds the eventual count on to the Start Date and returns the new date 
    SELECT @NewDate = DATEADD(d,@Count,@StartDate) 
    RETURN @NewDate 
END
GO
创建函数AddWorkDays
(    
@工作日为整数,
@StartDate作为日期时间
) 
返回日期时间
作为
开始
将@Count声明为Int
将@i声明为Int
将@NewDate声明为DateTime
设置@Count=0
设置@i=0
WHILE(@i<@WorkingDays)--运行要添加的天数
开始
--递增计数变量
选择@Count=@Count+1
--递增i变量
选择@i=@i+1
--将计数添加到StartDate,并检查此新日期是周六还是周日
--如果是星期六或星期日,则进入嵌套的while循环并递增count变量
而(1,7)中的DATEPART(工作日,DATEADD(d,@Count,@StartDate))
开始
选择@Count=@Count+1
结束
结束
--将最终计数添加到开始日期并返回新日期
选择@NewDate=DATEADD(d、@Count、@StartDate)
返回@NewDate
结束
去

这就是你要找的吗

declare @num_hours int; 
    set @num_hours = 1; 

select dateadd(HOUR, @num_hours, getdate()) as time_with_hour;

此问题已得到回答

以下内容将为日期增加N小时(周六和周日除外)

示例

Declare @Date1 datetime = '2017-04-28'
Declare @Hours int  = 36

Select D=max(D)
 From  (
        Select D,HN=-1+Row_Number() over (Order by D)
         From  (Select D=DateAdd(HOUR,-1+Row_Number() Over (Order By (select null)),@Date1) From  master..spt_values n1 ) D
         Where DateName(WEEKDAY,D) not in ('Saturday','Sunday') 
       ) D1
 Where HN=@Hours
返回

2017-05-01 12:00:00.000

@詹姆士工作日。不包括假日的天数(星期日、星期六)您最有可能创建一个包含假日信息的日历表。例如,每一天的行和信息,如果它是假日或不是。否则代码将非常复杂。您可能需要考虑一个表达式,使用工作日来确定要添加到日期的小时数。
2017-05-01 12:00:00.000