With T-SQL, I'm trying to find the easiest way to reverse the characters of a string without changing the position of the numeric value.
So for string:
abc223de11
have
edc223ba11
sql serverstringt-sql
With T-SQL, I'm trying to find the easiest way to reverse the characters of a string without changing the position of the numeric value.
So for string:
abc223de11
have
edc223ba11
Best Answer
I am not proud of this, but you can do it in T-SQL. This inline table-valued function breaks the string into a set of characters, and applies row numbers to just the string values, so that you can flip the set upside down.
The following call:
Yields the following results:
In SQL Server 2017, you can pack the string back together with predictable order using
STRING_AGG()
:In older versions, you'll need to use a
FOR XML
hack:Results in both cases: