Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

CHARINDEX problem Expand / Collapse
Author
Message
Posted Tuesday, July 2, 2013 4:50 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 4:01 PM
Points: 261, Visits: 550
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



Post #1469791
Posted Tuesday, July 2, 2013 5:19 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:40 PM
Points: 3,559, Visits: 7,677
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1469794
Posted Tuesday, July 2, 2013 5:57 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 7:18 PM
Points: 6,250, Visits: 7,409
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
Post #1469800
Posted Tuesday, July 2, 2013 9:33 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1469833
Posted Wednesday, July 3, 2013 4:42 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 4:01 PM
Points: 261, Visits: 550
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



Post #1470264
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse