将整数值分为HH:MM格式

时间:2014-10-28 09:57:15

标签: sql sql-server sql-server-2008 tsql

我有一个整数数据类型的列,它包含以下格式的持续时间。我想按照下表中的值分为HH和MM格式的这些值:

Time      HH     MM 
---------------------
3         00     03
17        00     17
207       02     07
786       07     86
1234      12     34

2 个答案:

答案 0 :(得分:1)

您可以使用字符串连接执行此操作:

select time / 100 as hh, mod(time, 100) as mm

如果你想把它们作为零填充字符串:

select right('00' + cast(time / 100), 2) as hh,
       right('00' + mod(time, 100), 2) as mm

答案 1 :(得分:0)

CREATE TABLE #tt
  (
     tim INT
  )

INSERT INTO #tt
VALUES     (3 ),
            (17 ),
            (207),
            (786),
            (1234 )    

SELECT tim,
           left(right('0000'+cast(tim AS varchar(4)),4),2),
           right(right('0000'+cast(tim AS varchar(4)),4),2)
    FROM   #tt