I need to convert this into this using PostgreSQL
dxItw9a4 --> DXiTW9A4
Is there any function or way that is already set?
I need to convert this into this using PostgreSQL
dxItw9a4 --> DXiTW9A4
Is there any function or way that is already set?
It sounds like the OP is looking for a SQL version of the
swapcase()
function found in many client-side languages. A few thoughts:swapcase()
on the client side. I'd do that.I figured I'd give #3 a try, and wrote some code. The kindest thing I can say about this function is that it works...but I doubt it's much good. I'm trying to beef up my PL/pgSQL skills, so any comments on improving the code welcomed. It's so much easier to write a lot of things in most other languages...but there are plenty of cases where pushing the logic onto the server is beneficial, I figure it's worth putting in some effort to get comfortable in PL/pgSQL. But, again, I'd use
swapcase()
, or a hand-rolled equivalent, on the client side, I'd expect.Here's a call:
And a longer call to show that it works:
Better
Here's a
swapcase()
function that uses GMB's code.You can use the following methods supported by PostreSQL:
Example:
Reference: http://www.postgresqltutorial.com/postgresql-letter-case-functions/
If you're only dealing with the characters A-Z, you can use the translate function in postgres to convert cases.
You can simplify it slightly be using the upper/lower functions.
Here is a solution that works by splitting the string into a resultset of charaters using
regexp_split_to_table()
, then converts them to the opposite case and joins them again using aggregate functionstring_agg()
:Demo on DB Fiddle: