2017-09-25 18 views
0

現在、あるテーブルからデータを取り出し、いくつかの制約に基づいて別のテーブルにデータを挿入しようとしています。 私は、スクリプトを実行すると残念ながら、私は次のエラーを取得:データ型nvarcharをfloatに変換するSQL INSERT INTO SELECTエラー

error converting data type nvarchar to 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' 

あなたは貸すことができ、任意のヘルプは大幅にそれは[Descripion]に連接されてい... [猫ステータス]で

+0

NVARCHARフィールドをFLOATに挿入しようとしています。 SELECTをチェックしてください。そうであれば、フィールドをFLOATに変換してください。 – galloleonardo

+0

しかし、ソーステーブルと最終テーブルの両方にはNVARCHARフィールドしかありません。どこでもFLOATを指定することはありませんので、何をすべきか分かりません。 フィールドを変換する場合は、どこで変換する必要がありますか?元のテーブルでは?または、クエリのどこかにcast()関数を置くことはできますか? – MichaelL

+2

ソースとデスティネーションのすべての列がvarchar(255)ですが、ケースステートメントでは、たとえばテストしています。 '[CAT Status] = 0'(1,2など)これは数値定数なので、[CAT Status]を変換して比較しようとします。 – SQLBadPanda

答えて

1

[CAT Status]行の1文字のように見えるので、sqlのキャスト時にエラーが発生します。 [CATステータス]変数をすべて確認できますか?

+0

私はソーステーブルのCATステータスのすべてのエントリを調べましたが、それらはすべて数字です。次のクエリを実行しました SELECT [CATステータス]からML_Pre_Test_Master IS_NUMERIC([CATステータス])= 0 行が返されませんでした。 – MichaelL

0

ルックをいただければ幸いです文字列。クエリの他の場所でその列への他の参照に基づいて、数値データ型を持つように見えます。数値データ型をconcatenateしようとすると、 "+"記号を使用すると、SQL Serverは数学を行うと考え、すべてを数値に変換しようとします。

+0

しかし、OPはすべての列が 'nvarchar'だと言っていますので、問題ではありません。 – Rokuto

+0

さて、私はすべての異なる連結を見て、フィールドが事前に変換されていることを確認する必要がありますか? どこでクエリを変換する必要がありますか? insert intoまたはselect文では?または別のセクションで別に? – MichaelL

+0

あなたは結末でそれをすぐにすることができます...+ CAST([Cat Status] AS NVARCHAR(10))+ ...または... CONCAT()関数を使用して文字列を作成し、必要なデータ型変換を処理できるようにすることができます。 –

関連する問題