Substring

  • Guys,

    I have names in the following format, is there any way to strip the name and grab caaXXXXX from the name. I have tried substring but could develop the logic to grab the text after last place holder

    caaxxxxx always occurs after the last place holder '_'

    John_l_smith_caa12000

    mike_caa12001

    Mark_m_andrew_levey_caa12002

    Any suggestions inputs would help.

    Thanks

  • DECLARE @s-2 varchar(255)

    SET @s-2 = 'Mark_m_andrew_levey_caa12002'

    SELECT REVERSE(LEFT(REVERSE(@s), CHARINDEX('a', REVERSE(@s)) - 1))

  • HI,

    Try this:

    SELECT RIGHT([MyVarchar],CHARINDEX('_',REVERSE([MyVarchar]))-1)

    Thanks

    Chris

    ----------------------------------------------
    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
  • try this

    DECLARE @s-2 varchar(255)

    SET @s-2 = 'Mark_m_andrew_levey_caa12002'

    SELECT substring(@s, charindex('_caa', @s-2)+1 , len(@s))

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

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