What is the best way to replace all '<' with <
in a given database column? Basically perform s/<[^;]/</gi
Notes:
- must work in MS SQL Server 2000
- Must be repeatable (and not end up with
<;;;;;;;;;
)
What is the best way to replace all '<' with <
in a given database column? Basically perform s/<[^;]/</gi
Notes:
<;;;;;;;;;
)
I think this can be done much cleaner if you use different STUFF :)
If MSSQL's regex flavor supports negative lookahead, that would be The Right Way to approach this.
will catch all instances of < which are not followed by a ; (even if they're followed by nothing, which [^;] would miss) and does not capture the following non-; character as part of the match, eliminating the issue mentioned in the comments on the original question of that character being lost in the replacement.
Unfortunately, I don't use MSSQL, so I have no idea whether it supports negative lookahead or not...
Very specific to this pattern, but I have done similar to this in the past:
REPLACE(REPLACE(columName, '<', '<'), '<', '<')
broader example (encode characters which may be inappropriate in a TITLE attribute)
Some hacking required but we can do this with LIKE, PATINDEX, LEFT AND RIGHT and good old string concatenation.
Better is that this is SQL Server version agnostic and should work just fine.
This article covers how to create a simple Regex Replace function that you can use in SQL 2000 (and 2005 with simple tweak) that can assist you.
How about:
Edit:
I just realized this will ignore columns with partially correct
<
strings.In that case you can ignore the second part of the where clause and call this afterward: