I would like to have a TSQL Statement to move Name Suffix (Jr, Sr., IV, etc) into another field.
The suffixes I see are
JR SR I II III IV V
Here is a sample of the data
LastName BRUNNING, II BURCH II BUSS, JR. CANI III CHRISTIAN,SR COLVIN Jr COWHERD,JR.
I would like the suffix moved out of the LastName field into another field called Suffix.
LastName Suffix BRUNNING II BURCH I BUSS JR CANI III CHRISTIAN SR COLVIN JR COWHERD JR
I am using SQL Server 2005 and can use SQL# functions.
Any help would be greatly appretiated.
You can probably do better than this using the SQL# functions, but in straight T-SQL, here you go.
The main idea here is to parse out the last segment/token in the name using
REVERSEandPATINDEX, and then match it to a list of known suffixes.First some test data:
Then, an inline
SELECTversion. Notice the use ofNULLIFto control forSUBSTRINGerrors.Alternately,
UPDATEw/ local vars:The inline
SELECTis fairly convenient, but difficult to read and troubleshoot. I prefer theUPDATEwith local vars for anything I might have to return to later. Plus, it makes individual edits easier to apply.EDIT,
SELECTmethod, slightly edited, and wrapped in an inline table-valued function. A inline TVF should be more efficient than a scalar UDF, and you get multiple return values to boot.