Guest User

Untitled

a guest
Aug 14th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. String pattern replace a column
  2. SELECT REPLACE(
  3. REPLACE(YourColumn, '$[%]foo##amet[%]$', 'amet'), '$[%]bar##[%]$', '')
  4.  
  5. SELECT REPLACE(REPLACE(YourColumn, ' $%amet%$ ', ' amet '), ' $%bar%$ ', ' ')
  6.  
  7. CREATE FUNCTION [dbo].[ReplaceWithDefault]
  8. (
  9. @InputString VARCHAR(4000)
  10. )
  11. RETURNS VARCHAR(4000)
  12. AS
  13. BEGIN
  14. DECLARE @Pattern VARCHAR(100) SET @Pattern = '$[%]_%##%[%]$'
  15. -- working copy of the string
  16. DECLARE @Result VARCHAR(4000) SET @Result = @InputString
  17. -- current match of the pattern
  18. DECLARE @CurMatch VARCHAR(500) SET @curMatch = ''
  19. -- string to replace the current match
  20. DECLARE @Replace VARCHAR(500) SET @Replace = ''
  21. -- start + end of the current match
  22. DECLARE @Start INT
  23. DECLARE @End INT
  24. -- length of current match
  25. DECLARE @CurLen INT
  26. -- Length of the total string -- 8001 if @InputString is NULL
  27. DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)
  28.  
  29. WHILE (PATINDEX('%' + @Pattern + '%', @Result) != 0)
  30. BEGIN
  31. SET @Replace = ''
  32.  
  33. SET @Start = PATINDEX('%' + @Pattern + '%', @Result)
  34. SET @CurMatch = SUBSTRING(@Result, @Start, @Len)
  35.  
  36. SET @End = PATINDEX('%[%]$%', @CurMatch) + 2
  37. SET @CurMatch = SUBSTRING(@CurMatch, 0, @End)
  38.  
  39. SET @CurLen = LEN(@CurMatch)
  40.  
  41. SET @Replace = REPLACE(RIGHT(@CurMatch, @CurLen - (PATINDEX('%##%', @CurMatch)+1)), '%$', '')
  42.  
  43. SET @Result = REPLACE(@Result, @CurMatch, @Replace)
  44. END
  45. RETURN(@Result)
  46. END
Add Comment
Please, Sign In to add comment