Suppose I have a string in my table, I now want to remove all characters that are the same as the first character (case-insensitive) from that string.
For example:
uJnMus onsyk uyT Uaszs -> JnMs onsyk yT aszs
@uJnMus o@nsyk uyT Ua@szs -> uJnMus onsyk uyT Uaszs
I know there is a very simple method, but it is not universally applicable (it requires adjusting the expression u
or @
or … based on the string):
SELECT regexp_replace(content, '^u|u', '', 'ig')
FROM string;
SELECT regexp_replace(content, '^@|@', '', 'ig')
FROM string;
I tried another method to apply to all strings:
SELECT regexp_replace(content, '^(.)|1', '', 'ig')
FROM string;
But the result is not as expected, it only removes the first character. I’m not particularly good at regular expressions.
Could anyone give me some help? (any other methods would also be appreciated)
3
Answers
Use
substr
to extract the first character, thenreplace
both the lower and upper form of the character with the empty string:Output:
This method can meet your needs:
https://dbfiddle.uk/vl7OfVUX
Use
left()
andreplace()
ortranslate()
:Case sensitive:
Case insensitive:
fiddle
Simpler and cheaper than regular expressions.
Works for any character.