Find first name?

  • Hi,

    I have a Manager_Name field which contains first and last name (example: 'Pam Smith').

    How can I write a query to list only first name?

    Thank you.

  • SELECT LEFT(Manager_Name, CHARINDEX(' ', Manager_Name, 1) -1)


    * Noel

  • I bet Jonathan will post PARSENAME stuff.

    In the meantime try

    
    
    SELECT LEFT(your_field,CHARINDEX(' ', your_field))

    this assumes that you always have a blank as separator and no leading blanks.

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Yup,

    
    
    SELECT LEFT(your_field,CHARINDEX(' ', your_field,1)-1)

    is better

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • quote:


    I bet Jonathan will post PARSENAME stuff.

    Frank


    No, Noel's way is better than PARSENAME for this as it ignores multiple word last names (like mine). Of course, some people down South have multiple word first names.

    --Jonathan



    --Jonathan

  • quote:


    Hi,

    I have a Manager_Name field which contains first and last name (example: 'Pam Smith').

    How can I write a query to list only first name?

    Thank you.


    Btw, any chance to change table design and split the information?

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • quote:


    ... it ignores multiple word last names (like mine). Of course, some people down South have multiple word first names.

    --Jonathan


    Which is why, when you're asked to do something like this, you make sure you explain what the results will be to the individual motivating the request.

    I normally enter "R David" as my first name in forms, and am mildly peeved when that simply gets shortened to "R" (note: I have never lived further south than my current residence of Columbus Ohio 🙂 ).

    If a name has three distinct words, all at least two characters long, it is impossible to guess 100% accurately for all valid names the right way to break up the data.

    Where can I see ParseName? I'd like to see how others have dealt with the issues involved.

    R David Francis


    R David Francis

  • Jonathan came up with a great solution to a different problem.

    Unfortunately I lost track of this thread, but you can always look at BOL

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Here is the answer to the multiple word firstname problem

    declare @name varchar(100)

    set @name = 'Marie Sarah Vigote'

    SELECT LEFT(@name, LEN(@name)-CHARINDEX(' ', REVERSE(@name), 1))

    Of cours is you have a name where you have multiple word first name AND multiple word lastname then you have a problem ...

    But I thing it is more frequent to have more the one firstname then more then one lastname (of cours Jonathan is an exception, as usual)

    Bye

    Gabor



    Bye
    Gabor

Viewing 9 posts - 1 through 8 (of 8 total)

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