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 ««123»»

extact last 3 characters from a string Expand / Collapse
Author
Message
Posted Wednesday, October 14, 2009 4:46 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Thursday, September 19, 2013 6:00 AM
Points: 715, Visits: 2,706
Does this help?

DECLARE @str NVARCHAR(10)

SET @str = 'ABCDEFGHI'

SELECT substring(@str, 1, len(@str) - 3) + ' ' + right(@str, 3)



Cheers,

J-F
Post #802628
Posted Wednesday, October 14, 2009 4:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 27, 2010 8:09 AM
Points: 28, Visits: 35
Many thanks
Post #802631
Posted Wednesday, October 14, 2009 5:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 27, 2010 8:09 AM
Points: 28, Visits: 35
thanks
Im stuck at another thing now
I have to do this conversion for all rows in the table
if i write select @string=str_col from tbl1
follwed by all the other string conversion statements;it gives me only the first row

How do i write the select command in the stored procedure statement to run for all rows in the table?
Post #802639
Posted Wednesday, October 14, 2009 5:14 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Thursday, September 19, 2013 6:00 AM
Points: 715, Visits: 2,706
Well then, don't use the variable, it was only for the demo. Use your column name instead, and select it directly from your table.

Cheers,

J-F
Post #802641
Posted Thursday, October 15, 2009 4:33 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 4:51 PM
Points: 833, Visits: 2,465
another method...

DECLARE @txt VARCHAR(20)

SELECT @txt = 'helloALL'

select STUFF(@txt,LEN(@txt)-2,0,' ')

Post #803875
Posted Thursday, October 15, 2009 9:45 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, May 15, 2014 5:11 PM
Points: 6,067, Visits: 5,283
ps. (6/17/2009)
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'.

I think all the ones after this one are just academic, they show different ways to accomplish the same thing, but THIS is the easiest AND the one he should use. He wants the last three characters.. Bang.. Done.

CEWII
Post #803932
Posted Sunday, October 18, 2009 12:17 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 4:51 PM
Points: 833, Visits: 2,465
my code was in response to this question:


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


Post #804808
Posted Sunday, October 18, 2009 7:30 PM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:53 PM
Points: 36,795, Visits: 31,257
SALIM ALI (6/18/2009)
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 ) )


Heh... Old post I know but I just have to ask... why would you do two relatively expensive "reverses" when RIGHT will do the job just fine? (I mean before this thread got "silly". )


--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 #804859
Posted Tuesday, October 20, 2009 1:14 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 28, 2013 8:19 AM
Points: 2, Visits: 11
If you wish to remove the last 3 characters you can do this in the select statement without modifing the table contents (select reverse(stuff(reverse(field_name),1,3,'fig'))Field_Name

I do this to rename all the uploads from jpg,doc,eml,mpg, etc for all files so i can show a gif file as the thumbnail before they view the full picture or document.

Hope this helps someone other than myself.
Post #805474
Posted Tuesday, October 20, 2009 5:51 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 4:53 PM
Points: 36,795, Visits: 31,257
That will certainly work and if you don't have many rows, you probably won't notice, but REVERSE is comparatively very expensive... it would be computationally cheaper if you just used STUFF and LEN alone by a fair bit.

SELECT STUFF(yourcolumn,LEN(yourcolumn-3),3,'GIF') which would probably be the fastest.


--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 #805607
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse