如何使用comman和double Apostrophe转换nvarchar(")

时间:2017-07-12 16:39:45

标签: sql-server

我将列注释作为nvarchar表,当我试图将nvarchar转换为int时,我得到的错误就像 转换varchar值时转换失败'" 2,9,10,25"'到数据类型int 请你帮帮我吗

1 个答案:

答案 0 :(得分:1)

如果您使用的是SQL Server 2016,则可以使用String_Split,否则您需要执行一些xml处理以进行拆分,如下所示:

>>> df[(df[['Rank']] == df[['Id', 'Rank']].groupby('Id').transform(max)).squeeze().tolist()]
      Id  Rank  Activity
0  14035     8  deployed
1  47728     8  deployed
3  24259     6       WIP
4  14251     8  deployed
6  14250     6       WIP

您的输入表

;With cte as (
    Select xm = CAST('<x>' + REPLACE((SELECT REPLACE(REPLACE(col1,'"',''),', ','$$$SSText$$$') AS [*] FOR XML PATH('')),'$$$SSText$$$','</x><x>')+ '</x>' AS XML) 
        from #test  
) Select [Value] from cte c
cross apply ( Select y.value(N'text()[1]', N'int') as value 
FROM c.xm.nodes(N'x') as x(y) ) a