nvarchar数据类型为datetime SQL查询

时间:2016-10-14 09:46:13

标签: sql-server datetime nvarchar

我有两张表ServiceTypeServices

Service表格结构

    ServiceID          int           Unchecked
    Service_TypeID     int           Unchecked
    Last_performed     date          Unchecked
    Frequency          nvarchar(50)  Unchecked
    Freq_Duration      nvarchar(50)  Unchecked
    Freq_Mileage       nvarchar(50)  Unchecked
    Remin_frequ        nvarchar(50)  Unchecked
    Remin_duration     nvarchar(50)  Unchecked
    Remin_Mileage      nvarchar(50)  Checked
    Next_due_datetime  datetime      Unchecked
    Next_due_mileage   nvarchar(50)  Unchecked

ServiceType结构

Service_TypeID  int             Unchecked
Service_Type    nvarchar(50)    Unchecked

示例数据

ServiceID   Service_TypeID      Last_performed  Frequency   Freq_Duration   Freq_Mileage    Remin_frequ Remin_duration  Remin_Mileage   Next_due_datetime   Next_due_mileage
3             2                 2012-12-01      6             month(s)       1000              1          day(s)          10            2013-03-10              1000

我想要这样的数据:

ServiceID   Service_TypeID      Last_performed  Service Frequency    Next Service      Create Reminder 
3             2                 2012-12-01      6 month(s)  1000     2013-03-10 1000    1 day(s) 10

为此,我尝试这个查询:

select  
    Services.ServiceID,
    ServiceType.Service_Type,
    Last_performed
    Services.Frequency+''+Freq_Duration+''+Freq_Mileage as [Service Frequency],
    Services.Next_due_datetime+''+Next_due_mileage as [Next Service],
    Services.Remin_frequ+''+Remin_duration+''+Remin_Mileage as [Create Reminder]
from 
    Services
inner join 
    ServiceType on ServiceType.Service_TypeID = Services.Service_TypeID

但是我收到了这个错误:

  

将nvarchar数据类型转换为日期时间数据类型会导致超出范围的值。

1 个答案:

答案 0 :(得分:0)

尝试使用concat

 concat(Services.Frequency,' ',Freq_Duration+' ',Freq_Mileage) as [Service Frequency]

否则需要使用强制转换或转换为varchar表示整数