How to Re Format DateTime in Sql with Ceiling or Flooring method?

你。 提交于 2019-12-25 04:10:52

问题


i need this table look please TABLE2



Table1

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



How can i change this table below table



Table2

VisitingCount Date
11-------------------15:00-15:30
6-------------------15:30-16:00

so, i writed some sql user-defined functions look below please:

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
 




So, i called this UDF in my sql query. So Result is correct.




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))... function method is not effective method. Because; writing too many "when-then" comparission is not sufficient way to take data. Is there any method like: "Math.Round()" in sql not C#.

For Example:

Run(11:28) result: 11:00 but Run(11:31) Result: 12:00


回答1:


I recommend you look at my response on your previous thread, but this seems to work:

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



回答2:


Would not be better to convert dates to number of minutes since midnight and use modulo to convert dates into period of time (half of hour in your case).

For example:

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

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

So SQL query would look something like:

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


来源:https://stackoverflow.com/questions/829825/how-to-re-format-datetime-in-sql-with-ceiling-or-flooring-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!