将联接子句中的数据类型varchar转换为数值时出错

时间:2019-02-21 11:13:30

标签: sql-server

请帮助进行此查询。我正在

  

在我的连接子句中将数据类型varchar转换为数值时出错。”

select 
       MARKETING
       ,HOME_TEL
       ,BUS_TEL 
       ,CEL_TEL 
       ,EMAIL   
       ,FAX 
       ,VALID_MAIL
       ,VALID_PHONE 
       ,VALID_SMS   
       ,VALID_EMAIL
       into [storagedb - baw].dbo.Geyser_Glynis
    from [storagedb - Mariana].dbo.HOC_Geyser_v2 as a
       left join [IIIDB].[dbo].[EEE_BASE_201901] as b
              on a.gcustomer_Number= b.Dedupe_Static

1 个答案:

答案 0 :(得分:0)

执行以下2个查询,以查找您试图连接在一起但不能转换为数字的值:

SELECT
    T.gcustomer_Number
FROM
    [storagedb - Mariana].dbo.HOC_Geyser_v2 AS T
WHERE
    TRY_CAST(T.gcustomer_Number AS NUMERIC) IS NULL

SELECT
    T.Dedupe_Static
FROM
    [IIIDB].[dbo].[EEE_BASE_201901] AS T
WHERE
    TRY_CAST(T.Dedupe_Static AS NUMERIC) IS NULL

您必须删除或更新它们才能正确转换为数字,或者,如果其中一列可能包含非数字值,则必须将数字列强制转换为VARCHAR,例如:

from 
    [storagedb - Mariana].dbo.HOC_Geyser_v2 as a
    left join [IIIDB].[dbo].[EEE_BASE_201901] as b on 
        CONVERT(VARCHAR(100), a.gcustomer_Number) = b.Dedupe_Static

请注意,对索引列应用转换或函数将使索引不适用,并且很有可能在全表扫描中结束。