如何显示过去 48 小时的军事时间?
How to show military hours for the last 48 hours?
我想以 30
分钟间隔显示最后 48
小时的军事时间,并显示日期。
例如:
Military_Hour Date
0000 2015-02-17
0030 2015-02-17
0100 2015-02-17
0130 2015-02-17
. .
. .
2330 2015-02-17
0000 2015-02-16
0030 2015-02-16
0100 2015-02-16
0130 2015-02-16
. .
. .
2330 2015-02-16
This answer 可能会帮助您将日期时间转换为军事时间。然后,您可以 select 只是 Military_Hour
的时间和 Date
的日期
This answer 用于查找 30 分钟间隔:
SELECT CONVERT(date, yourDate) AS `Date`,
CAST(DATEPART(HOUR, CONVERT(varchar(8),yourDate, 108) AS char(2)) + ':' + CAST(DATEPART(MINUTE, CONVERT(varchar(8), yourDate, 108)/30 AS char(2)), as time) AS `Military_Hour`
FROM yourTable
GROUP BY CAST(yourDate AS date),DATEPART(MINUTE, yourDate)/30
如果您想要 ####
格式而不是 ##:##
,您也可以删除连接的冒号
如果您希望从无到有地创建块,请使用此查询。如果您想 round/truncate 在 table 到 30 分钟的时间段内获取数据,那就是另一个问题了。
declare @baseDate date = '20150216';
select
/* SQL Server 2012 and later */
--format(timeBlock, 'HHmm') as Military_Hour,
--format(timeBlock, 'yyyy-MM-dd') as "Date",
/* or earlier versions */
timeBlock,
convert(char(10), timeBlock, 120) as "Date", /* one of many options */
cast(stuff(
substring(convert(varchar(20), timeBlock, 120), 12, 5),
3, 1, '') as char(5)) as Military_Hour /* one of many options */
from
(
select dateadd(mi, (u8.n * 6 + u6.n) * 30, cast(@baseDate as datetime)) as timeBlock
from
(
select 0 as n union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5
) as u6,
(
select 0 as n union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7
) as u8
) as blocks
order by
timeBlock
我想以 30
分钟间隔显示最后 48
小时的军事时间,并显示日期。
例如:
Military_Hour Date
0000 2015-02-17
0030 2015-02-17
0100 2015-02-17
0130 2015-02-17
. .
. .
2330 2015-02-17
0000 2015-02-16
0030 2015-02-16
0100 2015-02-16
0130 2015-02-16
. .
. .
2330 2015-02-16
This answer 可能会帮助您将日期时间转换为军事时间。然后,您可以 select 只是 Military_Hour
的时间和 Date
This answer 用于查找 30 分钟间隔:
SELECT CONVERT(date, yourDate) AS `Date`,
CAST(DATEPART(HOUR, CONVERT(varchar(8),yourDate, 108) AS char(2)) + ':' + CAST(DATEPART(MINUTE, CONVERT(varchar(8), yourDate, 108)/30 AS char(2)), as time) AS `Military_Hour`
FROM yourTable
GROUP BY CAST(yourDate AS date),DATEPART(MINUTE, yourDate)/30
如果您想要 ####
格式而不是 ##:##
如果您希望从无到有地创建块,请使用此查询。如果您想 round/truncate 在 table 到 30 分钟的时间段内获取数据,那就是另一个问题了。
declare @baseDate date = '20150216';
select
/* SQL Server 2012 and later */
--format(timeBlock, 'HHmm') as Military_Hour,
--format(timeBlock, 'yyyy-MM-dd') as "Date",
/* or earlier versions */
timeBlock,
convert(char(10), timeBlock, 120) as "Date", /* one of many options */
cast(stuff(
substring(convert(varchar(20), timeBlock, 120), 12, 5),
3, 1, '') as char(5)) as Military_Hour /* one of many options */
from
(
select dateadd(mi, (u8.n * 6 + u6.n) * 30, cast(@baseDate as datetime)) as timeBlock
from
(
select 0 as n union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5
) as u6,
(
select 0 as n union all select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all select 7
) as u8
) as blocks
order by
timeBlock