SQL Server:将int值转换为时间值

时间:2017-12-08 15:08:32

标签: sql sql-server type-conversion

在SQL Server查询中,我想将int转换为时间。

int表示小时和分钟(hhmm)。

例如:

1945 --> 19:45
30 --> 00:30
1 --> 00:01

使用您的解决方案修改后,此查询应返回true,true,true。

declare @int1 int = 0
declare @int2 int = 35
declare @int3 int = 1810

select 
    @int1 = '00:00', @int2 = '00:35', @int3 = '18:10'

2 个答案:

答案 0 :(得分:0)

尝试:

WITH Times AS(
    SELECT *
    FROM (VALUES (1945),(30),(1)) AS V(IntTime))
SELECT IntTime,
       CONVERT(time,STUFF(RIGHT('0000' + CONVERT(varchar(5),IntTime),4),3,0,':')) As TimeTime
FROM Times;

答案 1 :(得分:0)

declare @int1 int = 0
declare @int2 int = 35
declare @int3 int = 1805

declare @my_integer_value int

set @my_integer_value = @int1 -- Change to @int2 or @int3 as required.

select convert(char(8),convert
    (DATETIME,cast(@my_integer_value/100 as varchar(2))+':'+cast( @my_integer_value % 100 as varchar(2))+':00'
    ,108),108)
相关问题