How can I make a REPLACE PATTERN in SQL?












7















I have a long NVARCHAR variable where I need to replace some pattern like this:



DECLARE @data NVARCHAR(200) = 'Hello [PAT1] stackoverflow [PAT2] world [PAT3]'


I need to replace all [PAT%] with a blank space to look like:



'Hello stackoverflow world'


How can I do this using T-SQL in SQL Server 2008?



I was searching in other questions, and I only found this, but it doesn't help me, because I don't need to preserve de original part of the string.










share|improve this question





























    7















    I have a long NVARCHAR variable where I need to replace some pattern like this:



    DECLARE @data NVARCHAR(200) = 'Hello [PAT1] stackoverflow [PAT2] world [PAT3]'


    I need to replace all [PAT%] with a blank space to look like:



    'Hello stackoverflow world'


    How can I do this using T-SQL in SQL Server 2008?



    I was searching in other questions, and I only found this, but it doesn't help me, because I don't need to preserve de original part of the string.










    share|improve this question



























      7












      7








      7


      4






      I have a long NVARCHAR variable where I need to replace some pattern like this:



      DECLARE @data NVARCHAR(200) = 'Hello [PAT1] stackoverflow [PAT2] world [PAT3]'


      I need to replace all [PAT%] with a blank space to look like:



      'Hello stackoverflow world'


      How can I do this using T-SQL in SQL Server 2008?



      I was searching in other questions, and I only found this, but it doesn't help me, because I don't need to preserve de original part of the string.










      share|improve this question
















      I have a long NVARCHAR variable where I need to replace some pattern like this:



      DECLARE @data NVARCHAR(200) = 'Hello [PAT1] stackoverflow [PAT2] world [PAT3]'


      I need to replace all [PAT%] with a blank space to look like:



      'Hello stackoverflow world'


      How can I do this using T-SQL in SQL Server 2008?



      I was searching in other questions, and I only found this, but it doesn't help me, because I don't need to preserve de original part of the string.







      sql-server tsql replace






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 21:53









      jaco0646

      5,21452846




      5,21452846










      asked Jan 9 '13 at 23:19









      ElwiElwi

      553414




      553414
























          2 Answers
          2






          active

          oldest

          votes


















          11














          You may use this function for pattern replace. You can test it with this SQL-Fiddle demo to test.



          CREATE FUNCTION dbo.PatternReplace
          (
          @InputString VARCHAR(4000),
          @Pattern VARCHAR(100),
          @ReplaceText VARCHAR(4000)
          )
          RETURNS VARCHAR(4000)
          AS
          BEGIN
          DECLARE @Result VARCHAR(4000) SET @Result = ''
          -- First character in a match
          DECLARE @First INT
          -- Next character to start search on
          DECLARE @Next INT SET @Next = 1
          -- Length of the total string -- 8001 if @InputString is NULL
          DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)
          -- End of a pattern
          DECLARE @EndPattern INT

          WHILE (@Next <= @Len)
          BEGIN
          SET @First = PATINDEX('%' + @Pattern + '%', SUBSTRING(@InputString, @Next, @Len))
          IF COALESCE(@First, 0) = 0 --no match - return
          BEGIN
          SET @Result = @Result +
          CASE --return NULL, just like REPLACE, if inputs are NULL
          WHEN @InputString IS NULL
          OR @Pattern IS NULL
          OR @ReplaceText IS NULL THEN NULL
          ELSE SUBSTRING(@InputString, @Next, @Len)
          END
          BREAK
          END
          ELSE
          BEGIN
          -- Concatenate characters before the match to the result
          SET @Result = @Result + SUBSTRING(@InputString, @Next, @First - 1)
          SET @Next = @Next + @First - 1

          SET @EndPattern = 1
          -- Find start of end pattern range
          WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) = 0
          SET @EndPattern = @EndPattern + 1
          -- Find end of pattern range
          WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) > 0
          AND @Len >= (@Next + @EndPattern - 1)
          SET @EndPattern = @EndPattern + 1

          --Either at the end of the pattern or @Next + @EndPattern = @Len
          SET @Result = @Result + @ReplaceText
          SET @Next = @Next + @EndPattern - 1
          END
          END
          RETURN(@Result)
          END


          Resource link.






          share|improve this answer


























          • Thanks a lot!, for some reason I can't see, when I use brackets it doesn't work, but I'd changed them with parenthesis and it is working. I didn't know SQL Fiddle, is awesome!, thanks for it also.

            – Elwi
            Jan 10 '13 at 0:15






          • 1





            see this explanation of the brackets! You're welcome!

            – Nico
            Jan 10 '13 at 0:18



















          0














          +1 @Nico
          , I needed a function that will remove special charters from a string, so I adjusted your function a little bit to be able to do this:



          select dbo.PatReplaceAll('St. Martin`n tr‘an, or – in - the * field007', '[^0-9A-Z ]', '')
          --Returns 'St Martinn tran or in the field007'


          Here is the function:



          CREATE FUNCTION dbo.PatReplaceAll 
          (
          @Source varchar(8000),
          @Pattern varchar( 50),
          @Replace varchar( 100)
          )
          RETURNS varchar(8000)
          AS
          BEGIN
          if @Source is null or @Pattern is null or @Replace is null
          return null
          if PATINDEX('%' + @Pattern + '%', @Source) = 0
          return @Source
          -- Declare the return variable here
          DECLARE @Result varchar(8000) SET @Result = ''
          -- The remainder of the @Source to work on
          DECLARE @Remainder varchar(8000) SET @Remainder = @Source
          DECLARE @Idx INT

          WHILE (LEN(@Remainder) > 0)
          BEGIN
          SET @Idx = PATINDEX('%' + @Pattern + '%', @Remainder)
          IF @Idx = 0 --no match - return
          BEGIN
          SET @Result = @Result + @Remainder
          BREAK
          END
          -- Concatenate characters before the match to the result
          SET @Result = @Result + SUBSTRING(@Remainder, 1, @Idx - 1)
          -- Adjust the remainder
          SET @Remainder = SUBSTRING(@Remainder, @Idx, LEN(@Remainder) + 1 - @Idx)

          SET @Idx = 1
          -- Find the last char of the pattern (aka its length)
          WHILE PATINDEX(@Pattern, SUBSTRING(@Remainder, 1, @Idx)) = 0
          SET @Idx = @Idx + 1
          --remove the pattern from the remainder
          SET @Remainder = SUBSTRING(@Remainder, @Idx + 1, LEN(@Remainder) - @Idx)
          --Add the replace string
          SET @Result = @Result + @Replace
          END
          return @Result
          END
          GO





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14248244%2fhow-can-i-make-a-replace-pattern-in-sql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            11














            You may use this function for pattern replace. You can test it with this SQL-Fiddle demo to test.



            CREATE FUNCTION dbo.PatternReplace
            (
            @InputString VARCHAR(4000),
            @Pattern VARCHAR(100),
            @ReplaceText VARCHAR(4000)
            )
            RETURNS VARCHAR(4000)
            AS
            BEGIN
            DECLARE @Result VARCHAR(4000) SET @Result = ''
            -- First character in a match
            DECLARE @First INT
            -- Next character to start search on
            DECLARE @Next INT SET @Next = 1
            -- Length of the total string -- 8001 if @InputString is NULL
            DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)
            -- End of a pattern
            DECLARE @EndPattern INT

            WHILE (@Next <= @Len)
            BEGIN
            SET @First = PATINDEX('%' + @Pattern + '%', SUBSTRING(@InputString, @Next, @Len))
            IF COALESCE(@First, 0) = 0 --no match - return
            BEGIN
            SET @Result = @Result +
            CASE --return NULL, just like REPLACE, if inputs are NULL
            WHEN @InputString IS NULL
            OR @Pattern IS NULL
            OR @ReplaceText IS NULL THEN NULL
            ELSE SUBSTRING(@InputString, @Next, @Len)
            END
            BREAK
            END
            ELSE
            BEGIN
            -- Concatenate characters before the match to the result
            SET @Result = @Result + SUBSTRING(@InputString, @Next, @First - 1)
            SET @Next = @Next + @First - 1

            SET @EndPattern = 1
            -- Find start of end pattern range
            WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) = 0
            SET @EndPattern = @EndPattern + 1
            -- Find end of pattern range
            WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) > 0
            AND @Len >= (@Next + @EndPattern - 1)
            SET @EndPattern = @EndPattern + 1

            --Either at the end of the pattern or @Next + @EndPattern = @Len
            SET @Result = @Result + @ReplaceText
            SET @Next = @Next + @EndPattern - 1
            END
            END
            RETURN(@Result)
            END


            Resource link.






            share|improve this answer


























            • Thanks a lot!, for some reason I can't see, when I use brackets it doesn't work, but I'd changed them with parenthesis and it is working. I didn't know SQL Fiddle, is awesome!, thanks for it also.

              – Elwi
              Jan 10 '13 at 0:15






            • 1





              see this explanation of the brackets! You're welcome!

              – Nico
              Jan 10 '13 at 0:18
















            11














            You may use this function for pattern replace. You can test it with this SQL-Fiddle demo to test.



            CREATE FUNCTION dbo.PatternReplace
            (
            @InputString VARCHAR(4000),
            @Pattern VARCHAR(100),
            @ReplaceText VARCHAR(4000)
            )
            RETURNS VARCHAR(4000)
            AS
            BEGIN
            DECLARE @Result VARCHAR(4000) SET @Result = ''
            -- First character in a match
            DECLARE @First INT
            -- Next character to start search on
            DECLARE @Next INT SET @Next = 1
            -- Length of the total string -- 8001 if @InputString is NULL
            DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)
            -- End of a pattern
            DECLARE @EndPattern INT

            WHILE (@Next <= @Len)
            BEGIN
            SET @First = PATINDEX('%' + @Pattern + '%', SUBSTRING(@InputString, @Next, @Len))
            IF COALESCE(@First, 0) = 0 --no match - return
            BEGIN
            SET @Result = @Result +
            CASE --return NULL, just like REPLACE, if inputs are NULL
            WHEN @InputString IS NULL
            OR @Pattern IS NULL
            OR @ReplaceText IS NULL THEN NULL
            ELSE SUBSTRING(@InputString, @Next, @Len)
            END
            BREAK
            END
            ELSE
            BEGIN
            -- Concatenate characters before the match to the result
            SET @Result = @Result + SUBSTRING(@InputString, @Next, @First - 1)
            SET @Next = @Next + @First - 1

            SET @EndPattern = 1
            -- Find start of end pattern range
            WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) = 0
            SET @EndPattern = @EndPattern + 1
            -- Find end of pattern range
            WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) > 0
            AND @Len >= (@Next + @EndPattern - 1)
            SET @EndPattern = @EndPattern + 1

            --Either at the end of the pattern or @Next + @EndPattern = @Len
            SET @Result = @Result + @ReplaceText
            SET @Next = @Next + @EndPattern - 1
            END
            END
            RETURN(@Result)
            END


            Resource link.






            share|improve this answer


























            • Thanks a lot!, for some reason I can't see, when I use brackets it doesn't work, but I'd changed them with parenthesis and it is working. I didn't know SQL Fiddle, is awesome!, thanks for it also.

              – Elwi
              Jan 10 '13 at 0:15






            • 1





              see this explanation of the brackets! You're welcome!

              – Nico
              Jan 10 '13 at 0:18














            11












            11








            11







            You may use this function for pattern replace. You can test it with this SQL-Fiddle demo to test.



            CREATE FUNCTION dbo.PatternReplace
            (
            @InputString VARCHAR(4000),
            @Pattern VARCHAR(100),
            @ReplaceText VARCHAR(4000)
            )
            RETURNS VARCHAR(4000)
            AS
            BEGIN
            DECLARE @Result VARCHAR(4000) SET @Result = ''
            -- First character in a match
            DECLARE @First INT
            -- Next character to start search on
            DECLARE @Next INT SET @Next = 1
            -- Length of the total string -- 8001 if @InputString is NULL
            DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)
            -- End of a pattern
            DECLARE @EndPattern INT

            WHILE (@Next <= @Len)
            BEGIN
            SET @First = PATINDEX('%' + @Pattern + '%', SUBSTRING(@InputString, @Next, @Len))
            IF COALESCE(@First, 0) = 0 --no match - return
            BEGIN
            SET @Result = @Result +
            CASE --return NULL, just like REPLACE, if inputs are NULL
            WHEN @InputString IS NULL
            OR @Pattern IS NULL
            OR @ReplaceText IS NULL THEN NULL
            ELSE SUBSTRING(@InputString, @Next, @Len)
            END
            BREAK
            END
            ELSE
            BEGIN
            -- Concatenate characters before the match to the result
            SET @Result = @Result + SUBSTRING(@InputString, @Next, @First - 1)
            SET @Next = @Next + @First - 1

            SET @EndPattern = 1
            -- Find start of end pattern range
            WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) = 0
            SET @EndPattern = @EndPattern + 1
            -- Find end of pattern range
            WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) > 0
            AND @Len >= (@Next + @EndPattern - 1)
            SET @EndPattern = @EndPattern + 1

            --Either at the end of the pattern or @Next + @EndPattern = @Len
            SET @Result = @Result + @ReplaceText
            SET @Next = @Next + @EndPattern - 1
            END
            END
            RETURN(@Result)
            END


            Resource link.






            share|improve this answer















            You may use this function for pattern replace. You can test it with this SQL-Fiddle demo to test.



            CREATE FUNCTION dbo.PatternReplace
            (
            @InputString VARCHAR(4000),
            @Pattern VARCHAR(100),
            @ReplaceText VARCHAR(4000)
            )
            RETURNS VARCHAR(4000)
            AS
            BEGIN
            DECLARE @Result VARCHAR(4000) SET @Result = ''
            -- First character in a match
            DECLARE @First INT
            -- Next character to start search on
            DECLARE @Next INT SET @Next = 1
            -- Length of the total string -- 8001 if @InputString is NULL
            DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)
            -- End of a pattern
            DECLARE @EndPattern INT

            WHILE (@Next <= @Len)
            BEGIN
            SET @First = PATINDEX('%' + @Pattern + '%', SUBSTRING(@InputString, @Next, @Len))
            IF COALESCE(@First, 0) = 0 --no match - return
            BEGIN
            SET @Result = @Result +
            CASE --return NULL, just like REPLACE, if inputs are NULL
            WHEN @InputString IS NULL
            OR @Pattern IS NULL
            OR @ReplaceText IS NULL THEN NULL
            ELSE SUBSTRING(@InputString, @Next, @Len)
            END
            BREAK
            END
            ELSE
            BEGIN
            -- Concatenate characters before the match to the result
            SET @Result = @Result + SUBSTRING(@InputString, @Next, @First - 1)
            SET @Next = @Next + @First - 1

            SET @EndPattern = 1
            -- Find start of end pattern range
            WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) = 0
            SET @EndPattern = @EndPattern + 1
            -- Find end of pattern range
            WHILE PATINDEX(@Pattern, SUBSTRING(@InputString, @Next, @EndPattern)) > 0
            AND @Len >= (@Next + @EndPattern - 1)
            SET @EndPattern = @EndPattern + 1

            --Either at the end of the pattern or @Next + @EndPattern = @Len
            SET @Result = @Result + @ReplaceText
            SET @Next = @Next + @EndPattern - 1
            END
            END
            RETURN(@Result)
            END


            Resource link.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 23:03









            Community

            11




            11










            answered Jan 9 '13 at 23:56









            NicoNico

            873930




            873930













            • Thanks a lot!, for some reason I can't see, when I use brackets it doesn't work, but I'd changed them with parenthesis and it is working. I didn't know SQL Fiddle, is awesome!, thanks for it also.

              – Elwi
              Jan 10 '13 at 0:15






            • 1





              see this explanation of the brackets! You're welcome!

              – Nico
              Jan 10 '13 at 0:18



















            • Thanks a lot!, for some reason I can't see, when I use brackets it doesn't work, but I'd changed them with parenthesis and it is working. I didn't know SQL Fiddle, is awesome!, thanks for it also.

              – Elwi
              Jan 10 '13 at 0:15






            • 1





              see this explanation of the brackets! You're welcome!

              – Nico
              Jan 10 '13 at 0:18

















            Thanks a lot!, for some reason I can't see, when I use brackets it doesn't work, but I'd changed them with parenthesis and it is working. I didn't know SQL Fiddle, is awesome!, thanks for it also.

            – Elwi
            Jan 10 '13 at 0:15





            Thanks a lot!, for some reason I can't see, when I use brackets it doesn't work, but I'd changed them with parenthesis and it is working. I didn't know SQL Fiddle, is awesome!, thanks for it also.

            – Elwi
            Jan 10 '13 at 0:15




            1




            1





            see this explanation of the brackets! You're welcome!

            – Nico
            Jan 10 '13 at 0:18





            see this explanation of the brackets! You're welcome!

            – Nico
            Jan 10 '13 at 0:18













            0














            +1 @Nico
            , I needed a function that will remove special charters from a string, so I adjusted your function a little bit to be able to do this:



            select dbo.PatReplaceAll('St. Martin`n tr‘an, or – in - the * field007', '[^0-9A-Z ]', '')
            --Returns 'St Martinn tran or in the field007'


            Here is the function:



            CREATE FUNCTION dbo.PatReplaceAll 
            (
            @Source varchar(8000),
            @Pattern varchar( 50),
            @Replace varchar( 100)
            )
            RETURNS varchar(8000)
            AS
            BEGIN
            if @Source is null or @Pattern is null or @Replace is null
            return null
            if PATINDEX('%' + @Pattern + '%', @Source) = 0
            return @Source
            -- Declare the return variable here
            DECLARE @Result varchar(8000) SET @Result = ''
            -- The remainder of the @Source to work on
            DECLARE @Remainder varchar(8000) SET @Remainder = @Source
            DECLARE @Idx INT

            WHILE (LEN(@Remainder) > 0)
            BEGIN
            SET @Idx = PATINDEX('%' + @Pattern + '%', @Remainder)
            IF @Idx = 0 --no match - return
            BEGIN
            SET @Result = @Result + @Remainder
            BREAK
            END
            -- Concatenate characters before the match to the result
            SET @Result = @Result + SUBSTRING(@Remainder, 1, @Idx - 1)
            -- Adjust the remainder
            SET @Remainder = SUBSTRING(@Remainder, @Idx, LEN(@Remainder) + 1 - @Idx)

            SET @Idx = 1
            -- Find the last char of the pattern (aka its length)
            WHILE PATINDEX(@Pattern, SUBSTRING(@Remainder, 1, @Idx)) = 0
            SET @Idx = @Idx + 1
            --remove the pattern from the remainder
            SET @Remainder = SUBSTRING(@Remainder, @Idx + 1, LEN(@Remainder) - @Idx)
            --Add the replace string
            SET @Result = @Result + @Replace
            END
            return @Result
            END
            GO





            share|improve this answer




























              0














              +1 @Nico
              , I needed a function that will remove special charters from a string, so I adjusted your function a little bit to be able to do this:



              select dbo.PatReplaceAll('St. Martin`n tr‘an, or – in - the * field007', '[^0-9A-Z ]', '')
              --Returns 'St Martinn tran or in the field007'


              Here is the function:



              CREATE FUNCTION dbo.PatReplaceAll 
              (
              @Source varchar(8000),
              @Pattern varchar( 50),
              @Replace varchar( 100)
              )
              RETURNS varchar(8000)
              AS
              BEGIN
              if @Source is null or @Pattern is null or @Replace is null
              return null
              if PATINDEX('%' + @Pattern + '%', @Source) = 0
              return @Source
              -- Declare the return variable here
              DECLARE @Result varchar(8000) SET @Result = ''
              -- The remainder of the @Source to work on
              DECLARE @Remainder varchar(8000) SET @Remainder = @Source
              DECLARE @Idx INT

              WHILE (LEN(@Remainder) > 0)
              BEGIN
              SET @Idx = PATINDEX('%' + @Pattern + '%', @Remainder)
              IF @Idx = 0 --no match - return
              BEGIN
              SET @Result = @Result + @Remainder
              BREAK
              END
              -- Concatenate characters before the match to the result
              SET @Result = @Result + SUBSTRING(@Remainder, 1, @Idx - 1)
              -- Adjust the remainder
              SET @Remainder = SUBSTRING(@Remainder, @Idx, LEN(@Remainder) + 1 - @Idx)

              SET @Idx = 1
              -- Find the last char of the pattern (aka its length)
              WHILE PATINDEX(@Pattern, SUBSTRING(@Remainder, 1, @Idx)) = 0
              SET @Idx = @Idx + 1
              --remove the pattern from the remainder
              SET @Remainder = SUBSTRING(@Remainder, @Idx + 1, LEN(@Remainder) - @Idx)
              --Add the replace string
              SET @Result = @Result + @Replace
              END
              return @Result
              END
              GO





              share|improve this answer


























                0












                0








                0







                +1 @Nico
                , I needed a function that will remove special charters from a string, so I adjusted your function a little bit to be able to do this:



                select dbo.PatReplaceAll('St. Martin`n tr‘an, or – in - the * field007', '[^0-9A-Z ]', '')
                --Returns 'St Martinn tran or in the field007'


                Here is the function:



                CREATE FUNCTION dbo.PatReplaceAll 
                (
                @Source varchar(8000),
                @Pattern varchar( 50),
                @Replace varchar( 100)
                )
                RETURNS varchar(8000)
                AS
                BEGIN
                if @Source is null or @Pattern is null or @Replace is null
                return null
                if PATINDEX('%' + @Pattern + '%', @Source) = 0
                return @Source
                -- Declare the return variable here
                DECLARE @Result varchar(8000) SET @Result = ''
                -- The remainder of the @Source to work on
                DECLARE @Remainder varchar(8000) SET @Remainder = @Source
                DECLARE @Idx INT

                WHILE (LEN(@Remainder) > 0)
                BEGIN
                SET @Idx = PATINDEX('%' + @Pattern + '%', @Remainder)
                IF @Idx = 0 --no match - return
                BEGIN
                SET @Result = @Result + @Remainder
                BREAK
                END
                -- Concatenate characters before the match to the result
                SET @Result = @Result + SUBSTRING(@Remainder, 1, @Idx - 1)
                -- Adjust the remainder
                SET @Remainder = SUBSTRING(@Remainder, @Idx, LEN(@Remainder) + 1 - @Idx)

                SET @Idx = 1
                -- Find the last char of the pattern (aka its length)
                WHILE PATINDEX(@Pattern, SUBSTRING(@Remainder, 1, @Idx)) = 0
                SET @Idx = @Idx + 1
                --remove the pattern from the remainder
                SET @Remainder = SUBSTRING(@Remainder, @Idx + 1, LEN(@Remainder) - @Idx)
                --Add the replace string
                SET @Result = @Result + @Replace
                END
                return @Result
                END
                GO





                share|improve this answer













                +1 @Nico
                , I needed a function that will remove special charters from a string, so I adjusted your function a little bit to be able to do this:



                select dbo.PatReplaceAll('St. Martin`n tr‘an, or – in - the * field007', '[^0-9A-Z ]', '')
                --Returns 'St Martinn tran or in the field007'


                Here is the function:



                CREATE FUNCTION dbo.PatReplaceAll 
                (
                @Source varchar(8000),
                @Pattern varchar( 50),
                @Replace varchar( 100)
                )
                RETURNS varchar(8000)
                AS
                BEGIN
                if @Source is null or @Pattern is null or @Replace is null
                return null
                if PATINDEX('%' + @Pattern + '%', @Source) = 0
                return @Source
                -- Declare the return variable here
                DECLARE @Result varchar(8000) SET @Result = ''
                -- The remainder of the @Source to work on
                DECLARE @Remainder varchar(8000) SET @Remainder = @Source
                DECLARE @Idx INT

                WHILE (LEN(@Remainder) > 0)
                BEGIN
                SET @Idx = PATINDEX('%' + @Pattern + '%', @Remainder)
                IF @Idx = 0 --no match - return
                BEGIN
                SET @Result = @Result + @Remainder
                BREAK
                END
                -- Concatenate characters before the match to the result
                SET @Result = @Result + SUBSTRING(@Remainder, 1, @Idx - 1)
                -- Adjust the remainder
                SET @Remainder = SUBSTRING(@Remainder, @Idx, LEN(@Remainder) + 1 - @Idx)

                SET @Idx = 1
                -- Find the last char of the pattern (aka its length)
                WHILE PATINDEX(@Pattern, SUBSTRING(@Remainder, 1, @Idx)) = 0
                SET @Idx = @Idx + 1
                --remove the pattern from the remainder
                SET @Remainder = SUBSTRING(@Remainder, @Idx + 1, LEN(@Remainder) - @Idx)
                --Add the replace string
                SET @Result = @Result + @Replace
                END
                return @Result
                END
                GO






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 25 '18 at 19:30









                PlamenPlamen

                1043




                1043






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14248244%2fhow-can-i-make-a-replace-pattern-in-sql%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly