在SQL Server 2012中连接两个TIME列

时间:2013-12-04 04:52:22

标签: sql .net sql-server-2012

我想连接两个TIME列并显示为一列。

示例:

  • FromTime:9:00
  • ToTime:12:00

结果应该是:

9:00-12:00

3 个答案:

答案 0 :(得分:1)

通用SQL:

-- hh:mm:ss
SELECT 'result:' + CONVERT(CHAR(6), FromTime, 8) + '-' + CONVERT(CHAR(6), ToTime)
    FROM yourTable 

MySQL的:

-- hh:mm
SELECT 'result:' + DATE_FORMAT(FromTime, '%H:%i') + '-' + DATE_FORMAT(ToTime, '%H:%i')
    FROM yourTable

SQL Server:

-- hh:mm
SELECT 'result:' + convert(char(2), DATEPART(hh, FromTime)) + ':' + 
    CONVERT(CHAR(2), DATEPART(mm, FromTime)) + '-' +
    CONVERT(CHAR(2), DATEPART(hh, ToTime)) + ':' + 
    CONVERT(CHAR(2), DATEPART(mm, ToTime))
    FROM yourTable 

答案 1 :(得分:1)

declare @FromTime time  
declare @ToTime time

set @FromTime='9:00'
set @ToTime='12:00'


select cast(@FromTime as varchar(10))+ '-' + cast(@ToTime as varchar(10)) as result 

sql demo

答案 2 :(得分:0)

您可以使用convert

select convert(VARCHAR(5),getdate(),108) + ' - ' + convert(VARCHAR(5),getdate()-1,108)