简体   繁体   English

从字符串中删除字符

[英]Remove character from the string

I have a string, now I want to replace commas between the string. 我有一个字符串,现在我想替换字符串之间的逗号。

Declare @Query nvarchar(max)
Set @Query = 'Item1,Item2,"Item,Demo,3",New'

From the given string I want to remove comma from between double quotes("") 我想从给定的字符串中删除双引号(“”)之间的逗号

I want result like this 我想要这样的结果

'Item1,Item2,ItemDemo3,New'

Here this "Item,Demo,3" part now ItemDemo3 现在,此"Item,Demo,3"部分现在为ItemDemo3

The idea is to use PATINDEX to find a pattern. 这个想法是使用PATINDEX查找模式。 Here I have used patindex to find double quote and then used start and length of the desired substring. 在这里,我使用patindex查找双引号,然后使用所需子串的开始和长度。 There will be three portions: before, modified desired and after 将分为三个部分:之前,所需的修改和之后

Then I replaced the comma and made the string again 然后,我替换了逗号,并再次制作了字符串

    Declare @Query nvarchar(max) 

    Set @Query = 'Item1,Item2,"Item,Demo,3",New'

    Declare @start int, @len int
    SELECT @start = PATINDEX('%"%"%', @Query) + 1

    select @len=CHARINDEX('"', SUBSTRING(@Query, @start, LEN(@Query))) - 1

    select 
        SUBSTRING(@Query, 1, @start - 2) +
        REPLACE((SUBSTRING(@Query, @start, @len)), ',', '') +
        SUBSTRING(@Query, @start + @len + 1, LEN(@Query))

Please let me know whether it works.. 请让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM