如何在sqlserver 2008中连接2个时间数据类型?

时间:2014-03-11 23:07:38

标签: sql sql-server

我正在尝试连接两个start_timeend_time的时间,我收到此错误:'+'附近的语法不正确。我是什么我失踪了?这是我的查询

  SELECT schedule.sid AS [ID],
         doctor.dlname + ',' + doctor.dfname + ' ' + doctor.dmname AS [Physician],
         schedule.sday AS [Day],
         start_time + '-' + end_time , <----I get the error here
         doctor.rate AS [Rate] ,schedule.sstatus AS [Status] 
         FROM schedule INNER JOIN doctor ON schedule.did = doctor.did

2 个答案:

答案 0 :(得分:1)

试试这个......在连接它们之前,首先需要CAST start_timeend_time,因为只允许连接string数据类型。

SELECT schedule.sid AS [ID],
       doctor.dlname + ',' + doctor.dfname + ' ' + doctor.dmname AS [Physician],
       schedule.sday AS [Day],
       CAST(start_time AS VARCHAR) + '-' + CAST(end_time AS VARCHAR),
       doctor.rate AS [Rate] ,schedule.sstatus AS [Status] 
FROM schedule 
INNER JOIN doctor 
ON schedule.did = doctor.did

或者......你也可以使用CONVERT ......

SELECT schedule.sid AS [ID],
       doctor.dlname + ',' + doctor.dfname + ' ' + doctor.dmname AS [Physician],
       schedule.sday AS [Day],
       CONVERT(VARCHAR, start_time) + '-' + CONVERT(VARCHAR, end_time),
       doctor.rate AS [Rate] ,schedule.sstatus AS [Status] 
FROM schedule 
INNER JOIN doctor 
ON schedule.did = doctor.did

答案 1 :(得分:1)

SELECT   schedule.sid AS [ID]
        ,doctor.dlname + ',' + doctor.dfname + ' ' + doctor.dmname AS [Physician]
        ,schedule.sday AS [Day]
        ,CAST(start_time AS VARCHAR(8)) + '-' + CAST(end_time AS VARCHAR(8))  <----I get the error here
        ,doctor.rate AS [Rate] 
        ,schedule.sstatus AS [Status] 
FROM schedule INNER JOIN doctor 
ON schedule.did = doctor.did

start_time是Time / Datetime数据类型,'-'是一个varchar数据类型的字符串。连接两个值,以获得相同的数据类型。只需将您的六角形列转换为字符数据类型。

将时间字段转换为VARCHAR(8)将返回类似23:13:05的字符串。