Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Transforming a Name from First-Last to Last-First

RSS
Modified on Fri, Jun 12, 2009, 1:03 PM by Administrator Categorized as Algorithms, SQL Server
The following function will transform a name from first last to last, first form. For example, for "John Fitgerald Kennedy" it returns "Kennedy, John Fitzgerald".

NOTE: This transformation is not appropriate for names with a generational suffix, such as "Jr.", "III", etc. For example, the name "Martin Luther King, Jr." gets transformed to "Jr., Martin Luther King,"

create function [dbo].[TransformName]
    (
    @inputText varchar(max)
    ) returns varchar(max) as begin

declare 
     @result    varchar(max)
    ,@pos       int

select 
     @pos    = len(@inputText) - charindex(' ', reverse(@inputText), 1) + 2
    ,@result = substring(@inputText, @pos, 8000) + ', ' + substring(@inputText, 1, @pos-2)

return @result

end

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2017, Patrick Jasinski.