Split string and take last element
Rapidfire solution: To extract the last element from a delimited string in SQL, employ SUBSTRING and CHARINDEX:
Replace column_name with your field, table_name with your data source, and delimiter with the actual separator character.
Detailed Breakdown
Reversing the strings in the town
Enter the REVERSE function flipping the string, something like: "This is tremendous" to "suoerdne...". Yes, reversing is a strange world!
The CHARINDEX navigates our strange world, locates the position of the final delimiter.
Right swipe to the end
With RIGHT function now we have a portion of the string like the end piece of a "live-long" sandwich:
Playing with varying delimiters
When the separator has multiple personality disorder, we can do some magic with a CASE statement...abracadabra:
Tackling with not so cooperative strings
Then sometimes we have the grumpy strings with no delimiters or only one element. In this case, conditional checks can save your day:
Venturing outside SQL
On the other side of our SQLverse, similar techniques can be maneuvered in other programming languages.
References
Was this article helpful?