将十进制小时。分钟转换为hh:mm tt格式和副词

时间:2018-06-01 05:54:04

标签: sql sql-server

我需要将存储在hours.minutes中的十进制值(我相信值存储在hours.hundreds中)转换为hh:mm:tt等格式。

  • 11.00至11:00 AM
  • 11.02至11:01 AM
  • 11.03至11:02 AM
  • 11.05至11:03 AM
  • 11.07至11:04 AM
  • 11.08至11:05 AM

  • 11.10至11:06 AM

  • 11.12至11:07 AM
  • 11.13至11:08 AM
  • 11.15至11:09 AM
  • 11.17至11:10 AM

  • 11.18至11:11 AM

  • 11.20至11:12 AM
  • 11.22至11:13 AM
  • 11.23至11:14 AM
  • 11.25至11:15 AM

  • 11.27至11:16 AM

  • 11.28至11:17 AM
  • 11.30至11:18 AM
  • 11.32至11:19 AM
  • 11.33至11:20 AM
  • 11.35至11:21 AM
  • 11.37至11:22 AM
  • 11.38至11:23 AM
  • 11.40至11:24 AM
  • 11.42至11:25 AM

  • 11.50至11:30 AM

  • 11.58至11:35 AM
  • 11.67至11:40 AM
  • 11.75至11:45 AM
  • 11.83至11:50 AM
  • 11.92至11:55 AM

  • 23.00至11:00 PM

  • 23.02至11:01 PM
  • 23.03至11:02 PM
  • 23.05至11:03 PM
  • 23.07至11:04 PM
  • 23.08至11:05 PM等。

和viceverza

  • 晚上11:00至23.00
  • 11:01 PM至23.02等。

5 个答案:

答案 0 :(得分:2)

十进制到时间:

timefromparts(floor(x),(x % 1) * 60,0,0,0)

您应该使用TIME数据类型存储数据,仅用于显示您可以投射它的数据:

convert(varchar(7), timefromparts(floor(x),(x % 1) * 60,0,0,0), 100)

回到DECIMAL的时间很简单:

cast(datepart(hour,t) + (datepart(minute,t)/60.) as dec(4,2))

答案 1 :(得分:1)

此公式将按照描述给出完全匹配。

奇怪的常量.000147222用于舍入而不是截断:

DECLARE @ table(col1 decimal(4,2), col2 time(0))
INSERT @
values
('11.00','11:00 AM'),('11.02','11:01 AM'),('11.03','11:02 AM'),('11.05','11:03 AM'),
('11.07','11:04 AM'),('11.08','11:05 AM'),('11.10','11:06 AM'),('11.12','11:07 AM'),
('11.13','11:08 AM'),('11.15','11:09 AM'),('11.17','11:10 AM'),('11.18','11:11 AM'),
('11.20','11:12 AM'),('11.22','11:13 AM'),('11.23','11:14 AM'),('11.25','11:15 AM'),
('11.27','11:16 AM'),('11.28','11:17 AM'),('11.30','11:18 AM'),('11.32','11:19 AM'),
('11.33','11:20 AM'),('11.35','11:21 AM'),('11.37','11:22 AM'),('11.38','11:23 AM'),
('11.40','11:24 AM'),('11.42','11:25 AM'),('11.50','11:30 AM'),('11.58','11:35 AM'),
('11.67','11:40 AM'),('11.75','11:45 AM'),('11.83','11:50 AM'),('11.92','11:55 AM'),
('23.00','11:00 PM'),('23.02','11:01 PM'),('23.03','11:02 PM'),
('23.05','11:03 PM'),('23.07','11:04 PM'),('23.08','11:05 PM')



SELECT 
  stuff(stuff(convert(varchar(19),
    cast((col1)/24+.000147222 as datetime),0), 1,12,''), 6,0,' ') Calculatedcol2, 
  col2,
  cast(cast(cast(col2 as datetime) as float)*24 as decimal(4,2)) CalculatedCol1,
  col1
FROM @

答案 2 :(得分:0)

有点难看,但在我的SQL Server中工作:

DECLARE @decimal_time FLOAT 
SET @decimal_time = 23.08
SELECT SUBSTRING(CONVERT(VARCHAR(100), CAST( @decimal_time / 24.0 AS DATETIME), 100), 13, 10)

Result->11:04PM

DECLARE @DecTimeStr varchar(8)
SET @DecTimeStr = '11:01 PM'
SELECT ROUND(CAST(CONVERT(DATETIME, @DecTimeStr, 120) AS FLOAT) * 24, 2)

Result->23.02

答案 3 :(得分:0)

试试这些

select 
    format(dateadd(minute, cast(((value % 1) * 60 + 0.5) as int), dateadd(hour,  cast(value as int), '1900-01-01')), 'hh:mm tt')
from
    (values
        (11.00), (11.02), (11.03), (11.05), (11.07), (11.08), (11.10), (11.12), (11.13), (11.15),
        (11.17), (11.18), (11.20), (11.22), (11.23), (11.25), (11.27), (11.28), (11.30), (11.32),
        (11.33), (11.35), (11.37), (11.38), (11.40), (11.42), (11.50), (11.58), (11.67), (11.75),
        (11.83), (11.92), (23.00), (23.02), (23.03), (23.05), (23.07), (23.08)) as T(value);

select
    cast(cast(cast(cast(time as datetime) as float) * 24000 as int) / 1000.0 as decimal(4,2))
from
    (values
        ('11:00 AM'), ('11:01 AM'), ('11:02 AM'), ('11:03 AM'), ('11:04 AM'), ('11:05 AM'), ('11:06 AM'), ('11:07 AM'), ('11:08 AM'),
        ('11:09 AM'), ('11:10 AM'), ('11:11 AM'), ('11:12 AM'), ('11:13 AM'), ('11:14 AM'), ('11:15 AM'), ('11:16 AM'), ('11:17 AM'),
        ('11:18 AM'), ('11:19 AM'), ('11:20 AM'), ('11:21 AM'), ('11:22 AM'), ('11:23 AM'), ('11:24 AM'), ('11:25 AM'), ('11:30 AM'),
        ('11:35 AM'), ('11:40 AM'), ('11:45 AM'), ('11:50 AM'), ('11:55 AM'), ('11:00 PM'), ('11:01 PM'), ('11:02 PM'), ('11:03 PM'),
        ('11:04 PM'), ('11:05 PM')) as T(time)

答案 4 :(得分:-1)

也许......

a)从数百到时钟(经过测试):

SELECT TO_CHAR(TO_DATE('11.00', 'HH24.MI'), 'HH:MI AM') FROM DUAL;

b)从时钟到数百应该是:

SELECT TO_CHAR(TO_DATE('11:00 PM', 'HH:MI AM'), 'HH.MI') FROM DUAL;