CHARINDEX problem

  • Hello comunity

    I have a table name field FT.DESCAR Varchar(60) with the following content:

    5695 - 0 | 7050-127 MONTEMOR O NOVO

    with this Select :

    Select RIGHT(FT.DESCAR,CHARINDEX('|',FT.DESCAR)-1) from FT

    where ftstamp = 'jm13070255373,181882981'

    the result is:

    OR O NOVO

    to remove blank spaces i change my select to :

    select RIGHT( Ltrim(Rtrim(FT.DESCAR)),CHARINDEX('|', Ltrim(Rtrim(FT.DESCAR)))-1) from ft where ftstamp = 'jm13070255373,181882981'

    the result is the same:

    OR O NOVO

    I don´t understand why??

    someone could give me some help.

    Many thanks

    Luis Santos

  • What are you trying to do? It seems to me that you want either one of these options.

    Without sample data and expected results there's hard to know.

    Select LEFT(FT.DESCAR,CHARINDEX('|',FT.DESCAR)-1) from FT

    where ftstamp = 'jm13070255373,181882981'

    Select RIGHT(FT.DESCAR,CHARINDEX('|',REVERSE(FT.DESCAR))-1) from FT

    where ftstamp = 'jm13070255373,181882981'

    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
  • You're on target Luis. It's a CHARINDEX and right command usage confusion.

    OP, let's say you have this:

    1234|1234567890

    CHARINDEX (|) will = 5.

    When you RIGHT(string,5) you get 67890.

    What you need to do is RIGHT( string, LEN(string) - CHARINDEX()) to get to the correct # of characters, or use REVERSE() like Luis did above.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Luis Cazares (7/2/2013)


    What are you trying to do? It seems to me that you want either one of these options.

    Without sample data and expected results there's hard to know.

    Select LEFT(FT.DESCAR,CHARINDEX('|',FT.DESCAR)-1) from FT

    where ftstamp = 'jm13070255373,181882981'

    Select RIGHT(FT.DESCAR,CHARINDEX('|',REVERSE(FT.DESCAR))-1) from FT

    where ftstamp = 'jm13070255373,181882981'

    Just to share a trick for these "right splits"... you don't need the expense of a REVERSE.

    Select SUBSTRING(FT.DESCAR,CHARINDEX('|',FT.DESCAR)+1,8000) from FT

    where ftstamp = 'jm13070255373,181882981';

    --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)

  • Hello comunity

    First thanks for all of you for your replies.

    I try this:

    select substring('5695 - 0 | 7050-127 MONTEMOR O NOVO ',

    charindex('|','5695 - 0 | 7050-127 MONTEMOR O NOVO ')+1 ,

    len('5695 - 0 | 7050-127 MONTEMOR O NOVO ')) as [Last Name]

    and sql query return the desire value:

    7050-127 MONTEMOR O NOVO

    Note: the value '5695 - 0 | 7050-127 MONTEMOR O NOVO ' is the content of my table field FT.DESCAR.

    Best regards

    Luis Santos

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

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