SQL INSERT INTO SELECT错误,将数据类型nvarchar转换为float

时间:2017-09-25 12:25:02

标签: sql-server

我目前正在尝试从一个表中获取数据,并根据一些约束将其插入另一个表中。 不幸的是,当我运行脚本时,我收到以下错误:

  

将数据类型nvarchar转换为float

时出错

我不确定如何更改查询以解决此问题。源表和目标表都包含nvarchar(255)的所有字段,我不知道该怎么做。

SQL 查询如下:

Insert into Database.dbo.ML_Records ([AccountNumber]
  ,[House Number]
  ,[Street]
  ,[City]
  ,[Postalcode]
  ,[County]
  ,[Phone]
  ,[Country]
  ,[Account Type]
  ,[Nr_of_Dependents]
  ,[FirstName]
  ,[LasName]
  ,[Title]
  ,[JobTitle]
  ,[Email]
  ,[LeadType]
  ,[LeadSource]
  ,[LeadOrigin]
  ,[CampaignCode]
  ,[Contact_Preference]
  ,[Product_Interest]
  ,[NewServicesForCustomer]
  ,[Lead_Description])

Select  [Account Number]  as [AccountNumber]
    , NULL      as [House Number]
    , [Address Line 1] + ' ' + [Address Line 2]                                                                                                                                                     as [Street]
    , Town                                                                                                                                                                                          as [City]
    , Postcode                                                                                                                                                                                      as [Postalcode]
    , County                                                                                                                                                                                        as [County]
    , case when [Telephone] is null then [Mobile] else [Telephone] end                                                                                                                              as [Phone]
    , NULL                                                                                                                                                                                          as [Country]
    , case when [Account Type 2] is not null and [Identical Accounts] = 'No' then [Account Type 2] else [Account Type 1] end                        as [Account Type]
    , [Number of Dependents]                                        as [Nr_of_Employees_Company_Modelled]
    , [Forename]                                                                                                                                                                                    as [FirstName]
    , [Surname]                                                                                                                                                                                     as [LastName]
    , [Title]                                                                                                                                                                                       as [Title]
    , [Job Title]                                                                                                                                                                                   as [JobTitle]
    , case when [Email] is null then [Site Email] else [Email]  end                                                                                                                                 as [Email]
    , NULL                                                                                                                                                                                          as [LeadType]
    , NULL                                                                                                                                                                                          as [LeadSource]
    , NULL                                                                                                                                                                                          as [LeadOrigin]
    , case when ((([Email] is not NULL or [Site Email] is not null) and [Do NOT EMAIL] <> 'Y') and (([Telephone] is not null or [Mobile] is not null) and [DO NOT TELEPHONE] <> 'Y' and [Records on TPS] is NULL and [Records on CTPS] is NULL)) and [CAT Status] = 0 then 'A-B2F-CL-TM/EM-M2-CAT0-GL'
            when ((([Email] is not NULL or [Site Email] is not null) and [Do NOT EMAIL] <> 'Y') and (([Telephone] is not null or [Mobile] is not null) and [DO NOT TELEPHONE] <> 'Y' and [Records on TPS] is NULL and [Records on CTPS] is NULL)) and [CAT Status] = 1 then 'A-B2F-CL-TM/EM-M2-CAT1-GL'
            when ((([Email] is not NULL or [Site Email] is not null) and [Do NOT EMAIL] <> 'Y') and (([Telephone] is not null or [Mobile] is not null) and [DO NOT TELEPHONE] <> 'Y' and [Records on TPS] is NULL and [Records on CTPS] is NULL)) and [CAT Status] = 2 then 'A-B2F-CL-TM/EM-M2-CAT2-GL'
            when ((([Email] is not NULL or [Site Email] is not NULL) AND [Do NOT EMAIL] <> 'Y') and (([Telephone]  is NULL AND [Mobile] is NULL) OR [DO NOT TELEPHONE] = 'Y')) and [CAT Status] = 0 then 'A-B2F-CL-EM-M2-CAT0-GL'
            when ((([Email] is not NULL or [Site Email] is not NULL) AND [Do NOT EMAIL] <> 'Y') and (([Telephone]  is NULL AND [Mobile] is NULL) OR [DO NOT TELEPHONE] = 'Y')) and [CAT Status] = 1 then 'A-B2F-CL-EM-M2-CAT1-GL'
            when ((([Email] is not NULL or [Site Email] is not NULL) AND [Do NOT EMAIL] <> 'Y') and (([Telephone]  is NULL AND [Mobile] is NULL) OR [DO NOT TELEPHONE] = 'Y')) and [CAT Status] = 2 then 'A-B2F-CL-EM-M2-CAT2-GL'
            when ((([Email] is NULL and [Site Email] is null) or [Do NOT EMAIL] = 'Y') and (([Telephone] is not null or [Mobile] is not null) and [DO NOT TELEPHONE] <> 'Y' and [Records on TPS] is NULL and [Records on CTPS] is NULL)) and [CAT Status] = 0 then 'A-B2F-CL-TM-M2-CAT0-GL'
            when ((([Email] is NULL and [Site Email] is null) or [Do NOT EMAIL] = 'Y') and (([Telephone] is not null or [Mobile] is not null) and [DO NOT TELEPHONE] <> 'Y' and [Records on TPS] is NULL and [Records on CTPS] is NULL)) and [CAT Status] = 1 then 'A-B2F-CL-TM-M2-CAT1-GL'
            when ((([Email] is NULL and [Site Email] is null) or [Do NOT EMAIL] = 'Y') and (([Telephone] is not null or [Mobile] is not null) and [DO NOT TELEPHONE] <> 'Y' and [Records on TPS] is NULL and [Records on CTPS] is NULL)) and [CAT Status] = 2 then 'A-B2F-CL-TM-M2-CAT2-GL'
            when ((([Email] is NULL and [Site Email] is null) or [Do NOT EMAIL] = 'Y') and ((([Telephone] is NULL OR [DO NOT TELEPHONE] = 'Y') OR ([Records on TPS] = '1' or [Records on CTPS] = '1')) AND (([Mobile] is NULL OR [DO NOT TELEPHONE] = 'Y') OR ([Records on TPS] = '1' or [Records on CTPS] = '1')))) and [CAT Status] = 0 then 'A-B2F-CL-DM-M2-CAT0-GL'
            when ((([Email] is NULL and [Site Email] is null) or [Do NOT EMAIL] = 'Y') and ((([Telephone] is NULL OR [DO NOT TELEPHONE] = 'Y') OR ([Records on TPS] = '1' or [Records on CTPS] = '1')) AND (([Mobile] is NULL OR [DO NOT TELEPHONE] = 'Y') OR ([Records on TPS] = '1' or [Records on CTPS] = '1')))) and [CAT Status] = 1 then 'A-B2F-CL-DM-M2-CAT1-GL'
            when ((([Email] is NULL and [Site Email] is null) or [Do NOT EMAIL] = 'Y') and ((([Telephone] is NULL OR [DO NOT TELEPHONE] = 'Y') OR ([Records on TPS] = '1' or [Records on CTPS] = '1')) AND (([Mobile] is NULL OR [DO NOT TELEPHONE] = 'Y') OR ([Records on TPS] = '1' or [Records on CTPS] = '1')))) and [CAT Status] = 2 then 'A-B2F-CL-DM-M2-CAT2-GL' end as [CampaignCode]
    , case when ((([Email] is not NULL or [Site Email] is not null) and [Do NOT EMAIL] <> 'Y') and (([Telephone] is not null or [Mobile] is not null) and [DO NOT TELEPHONE] <> 'Y' and [Records on TPS] is NULL and [Records on CTPS] is NULL)) then 'EM/TM'
            when ((([Email] is not NULL or [Site Email] is not NULL) AND [Do NOT EMAIL] <> 'Y') and (([Telephone]  is NULL AND [Mobile] is NULL) OR [DO NOT TELEPHONE] = 'Y')) then 'EM'
            when ((([Email] is NULL and [Site Email] is null) or [Do NOT EMAIL] = 'Y') and (([Telephone] is not null or [Mobile] is not null) and [DO NOT TELEPHONE] <> 'Y' and [Records on TPS] is NULL and [Records on CTPS] is NULL)) then 'TM'
            when ((([Email] is NULL and [Site Email] is null) or [Do NOT EMAIL] = 'Y') and ((([Telephone] is NULL OR [DO NOT TELEPHONE] = 'Y') OR ([Records on TPS] = '1' or [Records on CTPS] = '1')) AND (([Mobile] is NULL OR [DO NOT TELEPHONE] = 'Y') OR ([Records on TPS] = '1' or [Records on CTPS] = '1')))) then 'DM' end as [Contact_Preference]

    , NULL                                                                                                                                  as [Product_Interest]
    , NULL                                                                                                                                  as [NewServicesForCustomer]
    , [CAT Status] +',' + [Competitor available 1] + ',' + [Competitor available 2] + ',' + [Competitor available 3] + ',' + [Competitor available 4] + ',' + [Competitor available 5] + ',' + [Competitor available 6] + ',' + [Competitor available 7] + ',' + [Competitor available 8] + ',' + [Competitor available 9] + ',' + [Last year accounts paid?] as [Description]
from ML_Pre_Test_Master
where [Commercial premise (yes/no)] <> 'Yes - Closed'

非常感谢您提供的任何帮助

2 个答案:

答案 0 :(得分:1)

看起来你的[CAT Status]行之一有一个字母,所以在施放sql时抛出错误。 你能检查所有[CAT Status]变量吗?

答案 1 :(得分:0)

查看[Cat Status] ...它正在连接到[Descripion]字符串中。根据查询中其他位置对该列的其他引用,它似乎具有数字数据类型。尝试使用“+”符号来合并数字数据类型,这使得SQL Server认为它应该进行数学运算并尝试将所有内容转换为数值。