从varchar转换为numeric时出错

时间:2018-05-17 11:23:06

标签: sql-server

我正在尝试运行此更新语句,但是,我收到一条错误,内容为“将varchar转换为numric时出错”。字段的数据类型如下:

dbo.keyona.customer_id_num is a decimal(16,0)
s.customer_identifier is varchar(250)
s.customer_cis is varchar(250)
k.dedupe_static is decimal(20,0

查询如下

update dbo.keyona
set dbo.keyona.customer_id_num = s.customer_identifier
from dbo.keyona k join [StorageDB - BAW].[dbo].[MI_NedLife_Mapper_Embedded_Linked_201804] s
on  s.customer_cis = k.dedupe_static

4 个答案:

答案 0 :(得分:2)

识别无法正确转换的记录:

SELECT
    s.customer_identifier
from 
    dbo.keyona k 
    join [StorageDB - BAW].[dbo].[MI_NedLife_Mapper_Embedded_Linked_201804] s on  s.customer_cis = k.dedupe_static
WHERE
    TRY_PARSE(s.customer_identifier AS decimal(16,0)) IS NULL

然后你必须要么过滤它们,要么修复它们以便正确转换它们。

答案 1 :(得分:1)

- 请尝试如下 - 您的数据类型应该相同。我认为你应该先解决这个问题。如果你不能请尝试下面 -

update dbo.keyona
set dbo.keyona.customer_id_num = CASE WHEN TRY_PARSE(s.customer_identifier as decimal(16,0)) IS NOT NULL THEN 
                                CAST(s.customer_identifier as decimal(16,0))
                                ELSE 
                                    dbo.keyona.customer_id_num
                                END
from dbo.keyona k join [StorageDB - BAW].[dbo].[MI_NedLife_Mapper_Embedded_Linked_201804] s
on  CAST(s.customer_cis as VARCHAR(500)) = CAST(k.dedupe_static as VARCHAR(500))

- 更好的解决方案是 -

update dbo.keyona
set dbo.keyona.customer_id_num = CAST(s.customer_identifier as decimal(16,0))                               
from dbo.keyona k join [StorageDB - BAW].[dbo].[MI_NedLife_Mapper_Embedded_Linked_201804] s
on  CAST(s.customer_cis as VARCHAR(500)) = CAST(k.dedupe_static as VARCHAR(500))
WHERE TRY_PARSE(s.customer_identifier as decimal(16,0)) IS NOT NULL

答案 2 :(得分:0)

update dbo.keyona
set dbo.keyona.customer_id_num = CAST(s.customer_identifier as decimal(16,0))                               
from dbo.keyona k join [StorageDB - BAW].[dbo].[MI_NedLife_Mapper_Embedded_Linked_201804] s
on  CAST(s.customer_cis as VARCHAR(500)) = CAST(k.dedupe_static as VARCHAR(500))
WHERE TRY_PARSE(s.customer_identifier as decimal(16,0)) IS NOT NULL

答案 3 :(得分:-1)

转换(十进制(16,0),s.customer_identifier)

您在两个地方都引用了customer_identifier

假设您在该列中没有字母字符。