Sql:請假跨月份問題,或跨年份問題 日期部分边界

---請假跨月份問題,或跨年份問題 日期部分边界
declare @sart datetime,@end datetime,@d int,@s1 datetime,@e1 datetime,@s2 datetime,@e2 datetime,@t varchar(50),@t2 varchar(50),@I int
set @sart='2012-11-18 14:56:12'
set @end='2012-12-08 14:56:12'
--set @I=DATEDIFF(ms,@sart,@end)
set @I=DATEDIFF(ss,@sart,@end)
if(year(@sart)=year(@end) and month(@sart)=month(@end))
begin
	set @d=day(@end)-day(@sart)+1
	--select @d=datediff(day,@sart,@end) 
        --set @d=@d*8 AS '小時'
       --select @d=datediff(hour,@sart,@end) AS '小時'
      --select @d=@I/86400000
	select @d=@I/86400	
end
else
begin
if(year(@sart)=year(@end) and month(@end)>month(@sart))
    set @t=cast(year(@sart) as varchar)+'-'+cast(month(@sart) as varchar)+'-'+cast(day(@sart) as varchar)+' '+cast(DATEPART(hour,@sart) as varchar)+':'+cast(DATEPART(minute,@sart) as varchar)+':'+cast(DATEPART(second,@sart) as varchar)
    select @s1=cast( @t as datetime)
--月末
    select @e1=DATEADD(day,  -1, DATEADD(month, 1,DATEADD(day,-DATEPART(day, @sart)+1,@sart )))
set @t2=cast(year(@end) as varchar)+'-'+cast(month(@end) as varchar)+'-'+cast(day(@end) as varchar)+' '+cast(DATEPART(hour,@end) as varchar)+':'+cast(DATEPART(minute,@end) as varchar)+':'+cast(DATEPART(second,@end) as varchar)
select @e2=cast( @t2 as datetime)
--月初
select @s2=DATEADD(month, 0,DATEADD(day,-DATEPART(day, @end)+1,@end))
--select @d=@I/86400000
select @d=@I/86400
end
select @I,@d,@s1,@e1,@s2,@e2





---
DECLARE @I INT
SET @I = DATEDIFF(ms,GETDATE()-RAND()*24,GETDATE())

SELECT
convert(varchar(10), (@I/86400000)) + ' Days  ' + 
convert(varchar(10), ((@I%86400000)/3600000)) + ' Hours  '+
convert(varchar(10), (((@I%86400000)%3600000)/60000)) + ' Mins  '+
convert(varchar(10), ((((@I%86400000)%3600000)%60000)/1000)) + ' sec ' +
convert(varchar(10), (((@I%86400000)%3600000)%1000)) + ' ms  ' AS [DD:HH:MM:SS:MS]
--
select
        *,
        Days          = datediff(dd,0,DateDif),
        Hours         = datepart(hour,DateDif),
        Minutes       = datepart(minute,DateDif),
        Seconds       = datepart(second,DateDif),
        MS            = datepart(ms,DateDif)
from
        (
        select
                DateDif = EndDate-StartDate,
                aa.*
        from
                (  -- Test Data
                Select
                        StartDate = convert(datetime,'20090213 02:44:37.923'),
                        EndDate   = convert(datetime,'20090715 13:24:45.837')
                ) aa
        ) a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值