如何使用Ceiling或Flooring方法在Sql中重新格式化DateTime?

时间:2009-05-06 14:18:05

标签: sql sql-server sql-server-2005

我需要这张表请看TABLE2


<小时/>

表1

 VisitingCount日期
1 ------------------- 15:09
3 ------------------- 15:10
7 ------------------- 15:15
1 ------------------- 15:39
2 ------------------- 15:40
3 ------------------- 15:47


<小时/>

如何更改表格下方的表格


<小时/>

表2

 VisitingCount日期
11 ------------------- 15:00-15:30
6 ------------------- 15:30-16:00

所以,我写了一些sql用户定义的函数,请看下面:

create FUNCTION [dbo].[fn_GetActivityLogsArranger]
(
@time AS nvarchar(max)
)

RETURNS  nvarchar(max)
AS
BEGIN
declare @Return varchar(30)

select @Return = case 
when @time between '00:00' and '00:30' then '00:00-00:30'
when @time between '00:30' and '01:00' then '00:30-01:00'
when @time between '01:00' and '01:30' then '01:00-01:30'

when @time between '01:30' and '02:00' then '01:30-02:00' 
when @time between '02:00' and '02:30' then '02:00-02:30' 
when @time between '02:30' and '03:00' then '02:30-03:00' 
when @time between '03:00' and '03:30' then '03:00-03:30' 
when @time between '03:30' and '04:00' then '03:30-04:00' 
when @time between '04:00' and '04:30' then '04:00-04:30' 
when @time between '04:30' and '05:00' then '04:30-05:00' 
when @time between '05:00' and '05:30' then '05:00-05:30'

when @time between '05:30' and '06:00' then '05:30-06:00' 
when @time between '06:00' and '06:30' then '06:00-06:30' 
when @time between '06:30' and '07:00' then '06:30-07:00' 
when @time between '07:00' and '07:30' then '07:00-07:30' 
when @time between '07:30' and '08:00' then '07:30-08:00' 
when @time between '08:00' and '08:30' then '08:00-08:30' 
when @time between '08:30' and '09:00' then '08:30-09:00' 
when @time between '09:00' and '09:30' then '09:00-09:30'

when @time between '09:30' and '10:00' then '09:30-10:00' 
when @time between '10:00' and '10:30' then '10:00-10:30' 
when @time between '10:30' and '11:00' then '10:30-11:00' 
when @time between '11:00' and '11:30' then '11:00-11:30' 
when @time between '11:30' and '12:00' then '11:30-12:00' 
when @time between '12:00' and '12:30' then '12:00-12:30' 
when @time between '12:30' and '13:00' then '12:30-13:00' 
when @time between '13:00' and '13:30' then '13:00-13:30'


when @time between '13:30' and '14:00' then '13:30-14:00' 
when @time between '14:00' and '14:30' then '14:00-14:30'
when @time between '14:30' and '15:00' then '14:30-15:00'
when @time between '15:00' and '15:30' then '15:00-15:30'
when @time between '15:30' and '16:00' then '15:30-16:00'
when @time between '16:00' and '16:30' then '16:00-16:30'
when @time between '16:00' and '16:30' then '16:00-16:30' 
when @time between '16:30' and '17:00' then '16:30-17:00' 
when @time between '17:00' and '17:30' then '17:00-17:30' 
when @time between '17:30' and '18:00' then '17:30-18:00'
when @time between '18:00' and '18:30' then '18:00-18:30' 


when @time between '18:30' and '19:00' then '18:30-19:00'
when @time between '19:00' and '19:30' then '19:00-19:30'
when @time between '19:30' and '20:00' then '19:30-20:00'
when @time between '20:00' and '20:30' then '20:00-20:30'
when @time between '20:30' and '21:00' then '20:30-21:00'
when @time between '21:00' and '21:30' then '21:00-21:30'
when @time between '21:30' and '22:00' then '21:30-22:00'
when @time between '22:00' and '22:30' then '22:00-22:30'
when @time between '22:30' and '23:00' then '22:30-23:00'
when @time between '23:00' and '23:30' then '23:00-23:30'
when @time between '23:30' and '24:00' then '23:30-:24:00'

else 'Unknown'
end
 Return @Return
end
 


<小时/>

所以,我在我的SQL查询中调用了这个UDF。所以结果是正确的。


<小时/>

select Count(Page) as VisitingCount,[Time]
from
( SELECT Page,Date,[user],
        dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),Date, 108)) as [Time]
        FROM scr_SecuristLog
) scr_SecuristLog
where
        Date between '2009-05-06' and '2009-05-07'
and
        [user] in
(       select USERNAME
        from scr_CustomerAuthorities
        where customerID=Convert(varchar,4)
        and ID=Convert(varchar,43)
)
group by [Time] order by [Time] asc 

dbo.fn_GetActivityLogsArranger(CONVERT(VARCHAR(5),Date,108))...函数方法不是有效的方法。 因为;写太多“当时 - 然后”的比较并不足以获取数据。  是否有任何方法:sql中的“Math.Round()”而不是C#。

例如:

跑步(11:28)结果:11:00但跑步(11:31)结果:12:

2 个答案:

答案 0 :(得分:1)

我建议您查看我之前的帖子中的回复,但这似乎有效:

declare @t datetime
set @t = '2009-05-06 12:29.997'
--truncate anything smaller than minute
set @t = cast (@t as smalldatetime)
select @t
select 
    case
        when datepart(minute, @t) >= 30
            then dateadd(minute, 60-(datepart(minute, @t)), @t)
        else dateadd(minute, -(datepart(minute, @t)), @t)
    end

答案 1 :(得分:0)

将日期转换为自午夜以来的分钟数并不是更好,并使用模数将日期转换为时间段(在您的情况下为一半小时)。

例如:

  • DATEDIFF(mi, '2009-05-06', '2009-05-06 00:15') / 30给出0

  • DATEDIFF(mi, '2009-05-06', '2009-05-06 00:45') / 30提供1

因此SQL查询看起来像:

DECLARE 
    @midnight AS smalldatetime

SET @midnight = dbo.truncatetime(GETDATE()) -- you have to write dbo.truncatetime()

SELECT
    SUM(VisitingCount) AS VisitingCount,
    expression to convert time in minutes to time period
FROM
    TABLE1
GROUP BY
    DATEDIFF(mi, @midnight, Date) / 30 -- should give 0, 1 for every next half of hour