转换失败的varchar值

时间:2018-07-17 08:43:45

标签: sql casting type-conversion varchar

   SELECT  
    ClientID,
    ProfileID as [Profile ID],
    Title,Name as [First Name],
    Surname as [Last Name],
    Lender,
    Product,
    LoanAmount as [Loan Amount],
    DateCompleted as [Mortgage Completed Date],
    Source as [Lead Source],
    ERC as [Erc Date],
    Casetype as [Case Type],
    email,
    HouseNameNum as Street,
    Address1 as Street, 
    Address2 as Street,
    Town as City,
    (postcode1 + postcode2) as Postcode,
    CAST('http://172.16.200.119/BPSAdmin/Legacy/CommTracking.aspx?ClientId='+ ClientID AS VARCHAR(100)) as [Perspectives Comms]
FROM Datatable

我收到的错误是:

  

转换varchar值时转换失败

'http://172.16.200.119/BPSAdmin/Legacy/CommTracking.aspx?ClientId='到数据类型int

ClientID是一个INT,但是,我想在字段的开头添加一些字符串以创建到客户端文件的超链接。

有关如何解决此问题的任何建议 干杯

2 个答案:

答案 0 :(得分:1)

我认为您需要自己明确地转换客户端ID列,因此只需将转换内容移至'http://172.16.200.119/BPSAdmin/Legacy/CommTracking.aspx?ClientId=' + cast(clientid as varchar)

答案 1 :(得分:1)

在强制类型转换之外添加url,以便强制转换功能正常工作并将Int转换为varchar

因为不能将Int和字符串连接在一起,所以它需要相同的数据类型。

SELECT  
    ClientID,
    ProfileID as [Profile ID],
    Title,Name as [First Name],
    Surname as [Last Name],
    Lender,
    Product,
    LoanAmount as [Loan Amount],
    DateCompleted as [Mortgage Completed Date],
    Source as [Lead Source],
    ERC as [Erc Date],
    Casetype as [Case Type],
    email,
    HouseNameNum as Street,
    Address1 as Street, 
    Address2 as Street,
    Town as City,
    (postcode1 + postcode2) as Postcode,
    'http://172.16.200.119/BPSAdmin/Legacy/CommTracking.aspx?ClientId='+ CAST( ClientID AS VARCHAR(100)) as [Perspectives Comms]
FROM Datatable