Does REPLACE function in SQL Server accept input from a table for 'string_pattern' parameter?

后端 未结 5 1365
清酒与你
清酒与你 2021-01-25 08:55

I\'m still learning my ropes with SQL Server and maybe this question sounds very naive/ridiculous. Please bear with me on this. :)

I saw a function in SQL Server defined

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-25 09:39

    A simple code to show you that the REPLACE can accept values from a table

    CREATE TABLE dbo.Test
    (
    RowID INT IDENTITY(1,1),
    Test VARCHAR(20)
    )
    
    GO
    
    INSERT INTO dbo.Test VALUES ('James')
    INSERT INTO dbo.Test VALUES ('John')
    
    GO
    
    ALTER FUNCTION [dbo].[fn_CleanNumeric]
        (
        @InputString VARCHAR(500),
        @Test VARCHAR(500)
        )
    RETURNS TABLE
    AS
    RETURN(
     SELECT
            REPLACE(@InputString, n.Test, '') AS Test
        FROM 
            dbo.Test n WHERE Test = @Test)
    GO
    
    SELECT * FROM [dbo].[fn_CleanNumeric] ('TestJohn','John')
    

    Result

    Test
    --------
    TestT
    

    The SELECT takes a random value and will check for that string pattern in the variable @InputString, thus passing a @Test variable ensures to REPLACE the right string pattern

提交回复
热议问题