CHARINDEX in reverse

  • I need to find the first and last names (separately).

    I am using:

    Left(Job_Descript.PM,CHARINDEX(' ',Job_Descript.PM) - 1)

    for the first name.

    Is there a change to use CHARINDEX from the right.

    Something to consider is that there is occasionally middle initials. If there wasn't I could simply start with the space and subtract from the LEN. But... the middle initial stops that idea.

    Thank you,

  • do you want something like this?

    declare @nam varchar(30)

    set @nam='pradeep singh'

    select left(@nam, charindex(' ',@nam)-1)

    select right(@nam, len(@nam)-charindex(' ',@nam))

    OUTPUT

    ------------

    ------------------------------

    pradeep

    (1 row(s) affected)

    ------------------------------

    singh

    (1 row(s) affected)



    Pradeep Singh

  • Is that going to work with the name: John Q. Public to give me:

    First Name = 'John'

    Last Name = 'Public'

  • Have you tried this.

    taking the above post as base code.

    REVERSE(left(REVERSE(@nam), charindex(' ',REVERSE(@nam))-1))

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • you could also use this for the surname:

    SUBSTRING(@nam,LEN(@nam) - charindex(' ',@nam),LEN(@nam))

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • I never thought of using a 'double reverse'

    Thank you very much, that was great and works no problem.

  • REVERSE is one of the less performant string functions in TSQL, so this alternative expression calls REVERSE only once.

    SELECT RIGHT(@nam, CHARINDEX(' ', REVERSE(@nam)) - 1)

    The expression above and also some of those in earlier posts will throw an error if the value of @nam has no spaces. If this is a possibility, you may want to use one of these more complex expression:

    /* Returns entire string if there are no spaces */

    SELECT RIGHT(@nam, COALESCE(NULLIF(CHARINDEX(' ', REVERSE(@nam)), 0) - 1, LEN(@nam)))

    /* Returns empty string if there are no spaces */

    SELECT RIGHT(@nam, COALESCE(NULLIF(CHARINDEX(' ', REVERSE(@nam)), 0) - 1, 0))

  • It is going to take me a bit to figure why/how the Coalese works with the reverse, etc..

    In the meantime since it saves on performance I have switched to it. Now comes the task of interpreting it.

    I appreciate your help,

    Thank you,

  • REVERSE is a fairly expensive function performance wise. No need to use three of them just to get the last name. I'll be back in a minute...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/2/2009)


    REVERSE is a fairly expensive function performance wise. No need to use three of them just to get the last name. I'll be back in a minute...

    Heh... I should read the whole post before I post... Andrew already did exactly what I would do... well, except on Tuesdays. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/2/2009) Andrew already did exactly what I would do...

    I would not.

    There was a member of Led Zeppelin, John Paul Jones.

    Would not be happy seeing his name after such "parsing".

    _____________
    Code for TallyGenerator

  • Sorry, did the same as Jeff, didn't read Andrews answer and posted the same solution. :hehe:

  • ...twice !! dozy ******

  • andrewd.smith (7/1/2009)


    REVERSE is one of the less performant string functions in TSQL, so this alternative expression calls REVERSE only once.

    SELECT RIGHT(@nam, CHARINDEX(' ', REVERSE(@nam)) - 1)

    Bear in mind that this only returns the last name if the last name contains no spaces. My last name consists of 2 words separated by a space and it is parsing routines such as this that send me mail to the last half of my name or file me under the wrong letter. Of course the real problem is the that the name is not stored in separate fields to begin with.



    Terri

    To speak algebraically, Mr. M. is execrable, but Mr. C. is
    (x+1)-ecrable.
    Edgar Allan Poe
    [Discussing fellow writers Cornelius Mathews and William Ellery Channing.]

  • Of course the real problem is the that the name is not stored in separate fields to begin with.

    Agreed.

Viewing 15 posts - 1 through 15 (of 18 total)

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