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


extact last 3 characters from a string


extact last 3 characters from a string

Author
Message
shiwani2002sg
shiwani2002sg
Old Hand
Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)Old Hand (319 reputation)

Group: General Forum Members
Points: 319 Visits: 97
i wish to extract last 3 characters of a string, in sql server 2005,.
substring doesnt accept -3 as length. so plz suggest some way

ex helloALL

output : ALL
Glenn Dorling
Glenn Dorling
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4052 Visits: 930
Have a look in Books Online regarding "String Functions". There's one there that does exactly what you want.
ps.
ps.
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10775 Visits: 3668
shiwani2002sg (6/17/2009)
i wish to extract last 3 characters of a string, in sql server 2005,.
substring doesnt accept -3 as length. so plz suggest some way

ex helloALL

output : ALL


Select RIGHT('helloALL',3) will give 'ALL'.



Pradeep Singh
SALIM ALI
SALIM ALI
SSC-Addicted
SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)

Group: General Forum Members
Points: 460 Visits: 248
you could try this, it's a bit crude but works...
declare @string varchar(50)
SET @string = 'ABCDEFGHIJ123'
select reverse ( substring ( reverse ( @string ) , 1 , 3 ) )



myheadhurts
myheadhurts
Mr or Mrs. 500
Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)

Group: General Forum Members
Points: 564 Visits: 635
select substring ('ABC123',4,3)



-MarkO

"You do not really understand something until you can explain it to your grandmother" - Albert Einstein
ps.
ps.
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10775 Visits: 3668
select substring(srcString, charindex(targetString, SrcString),len(srcString)-charindex(targetString, SrcString)+1)

This is a generic statement to be used in place of Right() where srcString is the main string and targetString is the string to be found from srcString.



Pradeep Singh
Florian Reischl
Florian Reischl
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13987 Visits: 3934
Just for fun - if RIGHT and CHARINDEX is prohibited but you are allowed to use a Tally table :-D

DECLARE @txt VARCHAR(20)

SELECT @txt = 'helloALL'

SELECT
SUBSTRING(@txt, N, 1)
FROM Tally
WHERE N BETWEEN LEN(@txt) - 2 AND LEN(@txt)
FOR XML PATH('')




The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
ps.
ps.
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10775 Visits: 3668
lol, lots of ideas to get last few characters from a string.:-D



Pradeep Singh
newbie_sql
newbie_sql
Old Hand
Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)Old Hand (308 reputation)

Group: General Forum Members
Points: 308 Visits: 35
Hi,

I have a slightly similar problem
I hav a nvarchar field with no particular format of where spaces are

I want to split the string as below
<all chars-3> space <last 3 chars>
Eg: "ABCDEFG" should appear as "ABCD EFG"
"ABC DEFGH" should appear as "ABCDE FGH" etc

How can i do this using string functions. Pls help. Thanks
SALIM ALI
SALIM ALI
SSC-Addicted
SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)SSC-Addicted (460 reputation)

Group: General Forum Members
Points: 460 Visits: 248
how about...

DECLARE @string VARCHAR(20)

SELECT @string = 'ABC DEFGH'
select @string = replace(@string,' ','')
select @string = substring ( @string, 1,(len(@string)-3)) + ' ' +
substring ( @string, (len(@string)-2),3 )
select @string



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