在SQL中转换为数字



我有两个表Stock 1和Stock 2,在Stock 1中有一个ID_1 (nvarchar(Max)数据类型)列,在Stock 2中有一个ID_2 (Decimal数据类型)列。

当ID_1与ID_2匹配时,在结果列中给出产品匹配ID

股票1

<表类>ID_1tbody><<tr>23495 _132212 _1778271 _26641066411662527 _2

使用TRY_CAST是否适用?

select isnull(s2.[Product Matching ID], s1.ID_1) [Product ID] 
from Stock_1 s1 
left Join Stock_2 s2 on s2.ID_2= try_cast(s1.ID_1 as int) 

您需要将字符串数据强制转换为整数:

select t2.product_matching_id
from table1 t1
inner join table2 t2
on t1.id_1 = (cast(isnull(t2.id_2,0)) as int)

您需要在查询中进行的更新将是:

Case 
when s1.ID_1 = (cast(isnull(s2.ID_2,0)) as int) 
then s2.Product_Matching_ID 
else s1.ID_1 end 
as [Product ID] from Stock_1 s1
left Join Stock_2 s2 on (cast(isnull(s2.ID_2,0)) as int) = s1.ID_1

相关内容

  • 没有找到相关文章

最新更新