If there’s only one :
in the field, an alternative is:
select regexp_replace(hostname,':[^:]+$', '') from hosts;
The regex has the character :
, and then a character class denied [^:]
(any character that nay be it :
), followed by quantifier +
(one or more occurrences). Next we have the bookmark $
, indicating the end of the string.
Which means he’s looking for :
followed by one or more characters other than :
, until it reaches the end of the string, and replaces all this with "nothing" (actually, an empty string), which is the same as removing this snippet.
See an example running in SQL Fiddle.
Just for the record, the regex you’re using does something completely different.
There’ll only be one
:
in the field?– hkotsubo
exactly, just one " : "inside the STRING
– Luis Henrique
Which bank? Oracle?
– hkotsubo
It’s actually an IBM DB2, but usually the same ORACLE or POSTGRESQL syntax works on it.
– Luis Henrique