need result of following query
select regexp_replace('StackOverflow', 'something', 'something')
as
stack_overflow
The following regex adds an underscore in front of every uppercase letter:
regexp_replace(name, '([A-Z])','_\1', 'g'))
As that results in on underscore at the beginning, this needs to be removed using trim()
trim(both '_' from lower(regexp_replace(name, '([A-Z])','_\1', 'g')))
The following query:
with names (name) as (
values ('StackOverflow'),
('Foo'),
('FooBar'),
('foobar'),
('StackOverflowCom')
)
select name, trim(both '_' from lower(regexp_replace(name, '([A-Z])','_\1', 'g'))) as new_name
from names;
returns:
name | new_name
-----------------+-------------------
StackOverflow | stack_overflow
Foo | foo
FooBar | foo_bar
foobar | foobar
StackOverflowCom | stack_overflow_com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With