我在SQL Server数据库中有十进制格式的数据,我想转换时间HH:MM格式。
例如:
3 to 3:00
3.25 to 3:15
3.5 to 3:30
3.75 to 3:45
非常感谢您的帮助。
答案 0 :(得分:3)
乘以60,并将该分钟数加到午夜:
declare @sample table (timeStoredWrong decimal(5,3))
insert into @sample (timeStoredWrong) values (3),(3.25),(3.5),(3.75)
select CONVERT(time,DATEADD(minute,60*timeStoredWrong,0)) as result
from @sample
结果:
result
----------------
03:00:00.0000000
03:15:00.0000000
03:30:00.0000000
03:45:00.0000000
答案 1 :(得分:1)
CREATE TABLE #tmp ([time] DECIMAL(6,2))
INSERT INTO #tmp VALUES (3)
INSERT INTO #tmp VALUES (3.25)
INSERT INTO #tmp VALUES (3.5)
INSERT INTO #tmp VALUES (3.75)
SELECT [time], CAST(CONVERT(TIME,DATEADD(SECOND, [time] * 3600, 0),108) AS TIME) FROM #tmp
DROP TABLE #tmp
答案 2 :(得分:1)
@Harzio,请参见此版本:您需要使用此功能CONVERT(CHAR(5), DATEADD(MINUTE, 60*[time], 0), 108)
完整的模拟如下:
DECLARE @tmp TABLE ([Time] FLOAT)
INSERT INTO @tmp
SELECT 3 UNION ALL
SELECT 3.25 UNION ALL
SELECT 3.5 UNION ALL
SELECT 3.75
SELECT
[Time],
NewTime=CONVERT(CHAR(5), DATEADD(MINUTE, 60*[time], 0), 108)
FROM @tmp
结果在这里:
答案 3 :(得分:0)
DECLARE @a money = 3.75;
SELECT FORMAT(FLOOR(@a)*100 + 60*(@a-FLOOR(@a)),'00:00')
答案 4 :(得分:0)
--set demo data here
Select * into #YourTable from
(SELECT 3.5 as atime union select 3.2833333333 union select 23.01666) dq;
--change string to float, multiply by 3600 to give seconds, add seconds
--to midnight date, format to HH:MM
SELECT atime, FORMAT(dateadd(s , round(3600 * CAST(atime as float),0) ,CAST('00:00:00' AS time)) ,N'hh\:mm') from #yourtable
--clear up demo data
drop table #yourtable