change name

  • IS there a script I can change firstname lastname to lastname, firstname:

    for example John Smith, to Smith, John

  • Not really. This assumes that nobody has a name like "Billy Joe" or a last name with spaces in it either... which is a gross simplification.

    SELECT LEFT(FullName,CHARINDEX(' ',FullName)-1) AS x

    , RIGHT(FullName,LEN(FullName)-CHARINDEX(' ',FullName)) AS y

    FROM

    (SELECT 'John Smith' AS FullName) x;

  • oops... double post...

  • Reformatting full names is a pain and something you can't really automate in the real world without some problems. Take this example:

    SELECT LEFT(FullName,CHARINDEX(' ',FullName)-1) AS x

    ,RIGHT(FullName,LEN(FullName)-CHARINDEX(' ',FullName)) AS y

    FROM

    (SELECT 'Sarah Michelle Gellar' AS FullName) x;

    You really should have different columns for first and last names. You could even add middle name and second last name.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply