SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CHARINDEX problem


CHARINDEX problem

Author
Message
luissantos
luissantos
SSC-Addicted
SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)

Group: General Forum Members
Points: 487 Visits: 749
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



Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16617 Visits: 19098
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
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8613 Visits: 7660
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86494 Visits: 41098
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
luissantos
luissantos
SSC-Addicted
SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)SSC-Addicted (487 reputation)

Group: General Forum Members
Points: 487 Visits: 749
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



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search