July 2, 2013 at 4:50 pm
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
July 2, 2013 at 5:19 pm
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'
July 2, 2013 at 5:57 pm
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.
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
July 2, 2013 at 9:33 pm
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
Change is inevitable... Change for the better is not.
July 3, 2013 at 4:42 pm
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