Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining index of last uppercase letter in column value (SQL)?

Short version: Is there a way to easily extract and ORDER BY a substring of values in a DB column based on the index (position) of the last upper case letter in that value, only using SQL?

Long version: I have a table with a username field, and the convention for usernames is the capitalized first initial of the first name, followed by the capitalized first initial of the last name, followed by the rest of the last name. As a result, ordering by the username field is 'wrong'. Ordering by a substring of the username value would theoretically work, e.g.

SUBSTRING(username,2, LEN(username))

...except that there are values with a capitalized middle initials between the other two initials. I am curious to know if, using only SQL (MS SQL Server,) there is a fairly straightforward / simple way to:

  1. Test the case of a character in a DB value (and return a boolean)
  2. Determine the index of the last upper case character in a string value

Assuming this is even remotely possible, I assume one would have to loop through the individual letters of each username to accomplish it, making it terribly inefficient, but if you have a magical shortcut, feel free to share. Note: This question is purely academic as I have since decided to go a much simpler way. I am just curious if this is even possible.

like image 214
David L. Avatar asked Jan 09 '23 07:01

David L.


1 Answers

  1. Test the case of a character in a DB value (and return a boolean)

SQL Server does not have a boolean datatype. bit is often used in its place.

DECLARE @Char CHAR(1) = 'f'

SELECT CAST(CASE
              WHEN @Char LIKE '[A-Z]' COLLATE Latin1_General_Bin
                THEN 1
              ELSE 0
            END AS BIT) 
 /* Returns 0 */

Note it is important to use a binary collation rather than a case sensitive collate clause with the above syntax. If using a CS collate clause the pattern would need to be spelled out in full as '[ABCDEFGHIJKLMNOPQRSTUVWXYZ]' to avoid matching lower case characters.

  1. Determine the index of the last upper case character in a string value
SELECT PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin, REVERSE('Your String'))
/* Returns one based index (6 ) */

SELECT PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin, REVERSE('no capitals'))
/* Returns 0 if the test string doesn't contain any letters in the range A-Z */
  1. To extract the surname according to those rules you can use
SELECT RIGHT(Name,PATINDEX('%[A-Z]%' COLLATE Latin1_General_Bin ,REVERSE(Name)))
FROM YourTable
like image 71
Martin Smith Avatar answered Feb 05 '23 07:02

Martin Smith