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


Select Command while dropping first 2 characters in the column


Select Command while dropping first 2 characters in the column

Author
Message
RICHTER-311234
RICHTER-311234
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 55
I am looking for a solution to a problem. I can't quite find the command I am looking for. I want to select from a column where the items in the column are like

ks105
ks103
ks106
and so on

I want my results to produce the
105
103
106
and so on.

I want my select command to drop the first 2 characters in that column when displaying the results. I do not want to modify the information in that table just want my select to display differently.

Thanks in advance

Richtoss
GilaMonster
GilaMonster
SSC Guru
SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)

Group: General Forum Members
Points: 370546 Visits: 46956
Lookup the RIGHT function in Books Online. To get the results you want, you may need to combine it with LEN to get all the string except the first two characters.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


andrewd.smith
andrewd.smith
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3636 Visits: 3232
You can use the STUFF or SUBSTRING functions as well as the RIGHT function. The SUBSTRING function version is likely to be the fastest, followed by the RIGHT version then the STUFF version.

DECLARE @value varchar(20)
SELECT @value = 'ks105'

SELECT STUFF(@value, 1, 2, '') AS [Stuff],
SUBSTRING(@value, 3, 18) AS [Substring],
RIGHT(@value, LEN(@value) - 2) AS [Right]


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