code_junkie

Perform regex (replace) in an SQL query

Nov 11th, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. create table test
  2. (
  3. id int identity(1, 1) not null,
  4. val varchar(25) not null
  5. )
  6.  
  7. insert into test values ('< <- ok, &lt <- nok')
  8.  
  9. while 1 = 1
  10. begin
  11. update test
  12. set val = left(val, patindex('%&lt[^;]%', val) - 1) +
  13. '<' +
  14. right(val, len(val) - patindex('%&lt[^;]%', val) - 2)
  15. from test
  16. where val like '%&lt[^;]%'
  17.  
  18. IF @@ROWCOUNT = 0 BREAK
  19. end
  20.  
  21. select * from test
  22.  
  23. create table test
  24. (
  25. id int identity(1, 1) not null,
  26. val varchar(25) not null
  27. )
  28.  
  29. insert into test values ('< <- ok, &lt <- nok')
  30.  
  31. WHILE 1 = 1
  32. BEGIN
  33. UPDATE test SET
  34. val = STUFF( val , PATINDEX('%&lt[^;]%', val) + 3 , 0 , ';' )
  35. FROM test
  36. WHERE val LIKE '%&lt[^;]%'
  37.  
  38. IF @@ROWCOUNT = 0 BREAK
  39. END
  40.  
  41. select * from test
  42.  
  43. s/&lt(?!;)/</gi
  44.  
  45. UPDATE tableName
  46. SET columName = REPLACE(columName , '&lt', '<')
  47. WHERE columnName LIKE '%lt%'
  48. AND columnName NOT LIKE '%lt;%'
  49.  
  50. UPDATE tableName
  51. SET columName = REPLACE(columName , '<;', '<')
Add Comment
Please, Sign In to add comment