从字符串中删除双下划线

时间:2017-09-07 20:34:36

标签: sql-server string tsql replace

我正在使用一系列REPLACE来删除基于内部规则集的特殊字符。

我首先在存储过程中多次替换一些字符,然后我对UDF进行交叉连接,传递剥离的值,沿着包含要删除的其他字符的正则表达式(不替换)

如果我这样做,则会正确操作以下值:

'O Sole Mio - Bass Clef Instrument

在应用多个REPLACE并调用udf后,我最终得到:

o_sole_mio_bass_clef_instrument

我遇到的问题是当我从存储过程中获取所有REPLACE并将它们放入UDF时,当我以这种方式运行代码时,会返回相同的值,如:

o_sole_mio__bass_clef_instrument

这有效:

存储过程

BEGIN

    SET NOCOUNT ON;
    DECLARE @workTable TABLE
    (
        [wtID]      INT IDENTITY PRIMARY KEY CLUSTERED
    ,   [sku]       VARCHAR(15)
    ,   [wtString]  VARCHAR(800)
    );

    INSERT  @workTable
    SELECT              
        1
    ,   REPLACE(
            REPLACE(
                REPLACE(
                    REPLACE(
                        REPLACE(
                            REPLACE(
                                REPLACE(
                                    REPLACE(
                                        REPLACE(
                                            LOWER('''O Sole Mio - Bass Clef Instrument')
                                        , CHAR(9), '')
                                    , '_', ' ')
                                , ' ', '_')
                            , '_-_', '_')
                        , '-', '_')
                    , '_&_', '_')
                , '=', '_')
            , '_/_', '_')
        , '/', '_')     

    SELECT      
        wt.sku
    ,   [wt].[wtString] AS 'ThisValueStillHasSomeCharactersTobeRemoved'
    ,   [MyResult] AS 'ThisValueIsTheExpectedResult'        
    FROM        
        @workTable [wt]     
    CROSS APPLY 
        [dbo].[RemoveValuesFromString]([wt].[wtString], '%[]~@#$%&*(),.,!¡^¿?:´''"/`+{},...,%')
    ORDER BY        
        [wt].[wtString]; 

END 

UDF:

ALTER FUNCTION [dbo].[RemoveValuesFromString]
(
    @SearchVal NVARCHAR(MAX)
,   @CharsToRemove NVARCHAR(MAX)
)
RETURNS TABLE

AS 

RETURN

WITH        [MyValues]      

AS 
( 
    SELECT
        SUBSTRING(@SearchVal, [N], 1) AS [MyChar]
    ,   [t].[N]
    FROM
        [cteTally] [t]
    WHERE
        [N] <= LEN(@SearchVal)
        AND CHARINDEX(SUBSTRING(@SearchVal, [N], 1), @CharsToRemove) = 0
)       

SELECT DISTINCT
    [MyResult] = STUFF((SELECT 
                            CAST([mv2].[MyChar] AS VARCHAR(MAX)) COLLATE SQL_Latin1_General_CP1251_CS_AS
                        FROM
                            [MyValues] [mv2]
                        ORDER BY
                            [mv2].[N]
                    FOR
                        XML PATH(''),   TYPE).[value]('.', 'NVARCHAR(MAX)'), 1, 0, '')
FROM
    [MyValues] [mv];

输出

sku ThisValueStillHasSomeCharactersTobeRemoved  ThisValueIsTheExpectedResult
1   'o_sole_mio_bass_clef_instrument            o_sole_mio_bass_clef_instrument

这不起作用:

存储过程

BEGIN
    SET NOCOUNT ON;
    DECLARE @workTable TABLE
    (
        [wtID]      INT IDENTITY PRIMARY KEY CLUSTERED
    ,   [sku]       VARCHAR(15)
    ,   [wtString]  VARCHAR(800)
    );

    INSERT  @workTable
    SELECT              
        1
    ,   '''O Sole Mio - Bass Clef Instrument'

    SELECT      
        wt.sku
    ,   [wt].[wtString] AS 'ThisValueStillHasSomeCharactersTobeRemoved'
    ,   [MyResult] AS 'ThisValueIsTheExpectedResult'        
    FROM        
        @workTable [wt]     
    CROSS APPLY 
        [dbo].[RemoveValuesFromString]([wt].[wtString], '%[]~@#$%&*(),.,!¡^¿?:´''"/`+{},...,%')
    ORDER BY        
        [wt].[wtString];  

END

UDF

ALTER FUNCTION [dbo].[RemoveValuesFromString]
(
    @SearchVal NVARCHAR(MAX)
,   @CharsToRemove NVARCHAR(MAX)
)
RETURNS TABLE       
AS      
RETURN      
WITH [MyValues]             
AS 
(   SELECT
        SUBSTRING(@SearchVal, [N], 1) AS [MyChar]
    ,   [t].[N]
    FROM
        [cteTally] [t]
    WHERE
        [N] <= LEN(@SearchVal)
        AND CHARINDEX(SUBSTRING(@SearchVal, [N], 1), @CharsToRemove) = 0
)
SELECT DISTINCT
    [MyResult] = STUFF((SELECT                                                                              
                            REPLACE(
                                REPLACE(
                                    REPLACE(
                                        REPLACE(
                                            REPLACE(
                                                REPLACE(
                                                    REPLACE(
                                                        REPLACE(
                                                            REPLACE(
                                                                LOWER(CAST([mv2].[MyChar] AS VARCHAR(MAX)) COLLATE SQL_Latin1_General_CP1251_CS_AS)
                                                            , CHAR(9), '')
                                                        , '_', ' ')
                                                    , ' ', '_')
                                                , '_-_', '_')
                                            , '-', '_')
                                        , '_&_', '_')
                                    , '=', '_')
                                , '_/_', '_')
                            , '/', '_')                                                                                 
                        FROM
                            [MyValues] [mv2]
                        ORDER BY
                            [mv2].[N]
                    FOR
                        XML PATH(''),   TYPE).[value]('.', 'NVARCHAR(MAX)'), 1, 0, '')
FROM
    [MyValues] [mv];

输出

sku ThisValueStillHasSomeCharactersTobeRemoved  ThisValueIsTheExpectedResult
1   'O Sole Mio - Bass Clef Instrument          o_sole_mio___bass_clef_instrument

我希望我正确地提出这个问题,我想我只是在忽略一些非常简单的事情。我真的很感激一些帮助。

非常感谢。

0 个答案:

没有答案