SQL 获取两个时间小时部分之差

yghuaa 2013-12-23 01:26:49
create function gethours(@stime datetime,@etime datetime)
returns int
as
begin
declare @hstime nvarchar(30)
declare @hetime nvarchar(30)
declare @result int
select @hstime=Ltrim(datepart(hh,@stime))
select @hetime=Ltrim(datepart(hh,@etime))
if (@hstime<8 and @hetime<17 and @hetime>7)
begin
set @result=@hetime-8
end
else if (@hstime<8 and @hetime>17)
begin
set @result=8
end
else if (@hstime<8 and @hetime<8)
begin
set @result=0
end
else if (@hstime>=17 and @hetime>=17)
begin
set @result=0
end
else if (@hstime>=17 and @hetime<17 and @hetime>=8)
begin
set @result=@hetime-8
end
else if (@hstime>=17 and @hetime<8)
begin
set @result=0
end
else if (@hstime>=8 and @hstime<17 and @hetime>=17)
begin
set @result=17-@hstime
end
else if (@hstime>=8 and @hstime<=17 and @hetime<8)
begin
set @result=17-@hstime
end
else if (@hstime>@hetime AND @hetime>=8 AND @hstime <17)
begin
set @result=cast(@hetime as int)-cast(@hstime as int)+9
end
else if (@hstime>@hetime AND @hetime>=8 AND @hstime >17)
begin
set @result=cast(@hetime as int)-8
end
else
begin
set @result=(cast(@hetime as int)- cast(@hstime as int))
end
return @result
end

我执行这个SQL的时候,为什么只是返回else中的结果,我在满足(@hstime>@hetime AND @hetime>=8 AND @hstime <17

的条件后,例如 stime:‘2013-12-06 16:22:28.840’ etime: ‘2013-12-11 08:39:39.270’ 返回的是-8??
...全文
367 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
yghuaa 2013-12-23
  • 打赏
  • 举报
回复
引用 18 楼 DBA_Huangzj 的回复:
2010也有的,不过我本机没装....不需要用
感谢!
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
vs怎么用我就不记得了,你自己研究吧,2008 是有的,同样是绿色的三角形,除非安装过程少勾选了什么,我是完全安装,所以也不知道哪个了
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
2010也有的,不过我本机没装....不需要用
yghuaa 2013-12-23
  • 打赏
  • 举报
回复
引用 16 楼 DBA_Huangzj 的回复:
sqlserver没有2010,你用的是vs吧?vs更不可能没有调试功能,不然怎么开发啊
对VS是2010的,SQL Server是2008的
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
sqlserver没有2010,你用的是vs吧?vs更不可能没有调试功能,不然怎么开发啊
yghuaa 2013-12-23
  • 打赏
  • 举报
回复
引用 14 楼 DBA_Huangzj 的回复:
sql server management studio 2012
额 2010版的似乎没有debug功能呢
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
sql server management studio 2012
yghuaa 2013-12-23
  • 打赏
  • 举报
回复
[quote=引用 12 楼 DBA_Huangzj 的回复:] [/quot厚颜无耻的再问下,你这个是什么软件啊?
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
yghuaa 2013-12-23
  • 打赏
  • 举报
回复
引用 8 楼 DBA_Huangzj 的回复:
跟踪了一下,你的代码实际上执行了: set @result=(cast(@hetime as int)- cast(@hstime as int)) 这一句
太感谢了,请问要怎么跟踪SQL的执行过程的??
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
DECLARE @hstime INT--NVARCHAR(30) DECLARE @hetime INT--NVARCHAR(30) 你的类型用错了
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
发现问题了,用这句:

	alter FUNCTION gethours
    (
      @stime DATETIME ,
      @etime DATETIME
    )
RETURNS INT
AS
    BEGIN
        DECLARE @hstime INT--NVARCHAR(30)
        DECLARE @hetime INT--NVARCHAR(30)
        DECLARE @result INT
        SELECT  @hstime = LTRIM(DATEPART(hh, @stime))
        SELECT  @hetime = LTRIM(DATEPART(hh, @etime))
        IF ( @hstime < 8
             AND @hetime < 17
             AND @hetime > 7
           )
            BEGIN
                SET @result = @hetime - 8
            END
        ELSE
            IF ( @hstime < 8
                 AND @hetime > 17
               )
                BEGIN
                    SET @result = 8
                END  
            ELSE
                IF ( @hstime < 8
                     AND @hetime < 8
                   )
                    BEGIN
                        SET @result = 0
                    END
                ELSE
                    IF ( @hstime >= 17
                         AND @hetime >= 17
                       )
                        BEGIN
                            SET @result = 0
                        END
                    ELSE
                        IF ( @hstime >= 17
                             AND @hetime < 17
                             AND @hetime >= 8
                           )
                            BEGIN
                                SET @result = @hetime - 8
                            END
                        ELSE
                            IF ( @hstime >= 17
                                 AND @hetime < 8
                               )
                                BEGIN
                                    SET @result = 0
                                END
                            ELSE
                                IF ( @hstime >= 8
                                     AND @hstime < 17
                                     AND @hetime >= 17
                                   )
                                    BEGIN
                                        SET @result = 17 - @hstime
                                    END
                                ELSE
                                    IF ( @hstime >= 8
                                         AND @hstime <= 17
                                         AND @hetime < 8
                                       )
                                        BEGIN
                                            SET @result = 17 - @hstime
                                        END
                                    ELSE
                                        IF ( @hstime > @hetime
                                             AND @hetime >= 8
                                             AND @hstime < 17
                                           )
                                            BEGIN
                                                SET @result = CAST(@hetime AS INT)
                                                    - CAST(@hstime AS INT) + 9 
                                            END
                                        ELSE
                                            IF ( @hstime > @hetime
                                                 AND @hetime >= 8
                                                 AND @hstime > 17
                                               )
                                                BEGIN
                                                    SET @result = CAST(@hetime AS INT)
                                                        - 8
                                                END
                                            ELSE
                                                BEGIN
                                                    SET @result = ( CAST(@hetime AS INT)
                                                              - CAST(@hstime AS INT) )
                                                END
        RETURN @result
    END
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
跟踪了一下,你的代码实际上执行了: set @result=(cast(@hetime as int)- cast(@hstime as int)) 这一句
yghuaa 2013-12-23
  • 打赏
  • 举报
回复
引用 5 楼 DBA_Huangzj 的回复:
16-8=-8 没问题啊
要执行@result=cast(@hetime as int)-cast(@hstime as int)+9 返回1的结果才对啊
yghuaa 2013-12-23
  • 打赏
  • 举报
回复
引用 3 楼 yupeigu 的回复:
哦,因为你的函数,一开始取出了这2个日期的小时,然后进行比较,所以才会导致返回-8
我就是要取出小时比较啊,满足else if的条件,但是没返回@result=cast(@hetime as int)-cast(@hstime as int)+9 这个结果
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
16-8=-8 没问题啊
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
我觉得你的函数不一定要改,单数输入时要先判断是否跨天
LongRui888 2013-12-23
  • 打赏
  • 举报
回复
哦,因为你的函数,一开始取出了这2个日期的小时,然后进行比较,所以才会导致返回-8
yghuaa 2013-12-23
  • 打赏
  • 举报
回复
引用 1 楼 DBA_Huangzj 的回复:
因为你只计算了hour,没有考虑跨天的
跨天的我用另外一个函数实现。 但是这里只比较小时的大小,和天数没关系啊
發糞塗牆 2013-12-23
  • 打赏
  • 举报
回复
因为你只计算了hour,没有考虑跨天的

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧