?
Solved

String selection charindex

Posted on 2006-06-28
2
Medium Priority
?
458 Views
Last Modified: 2008-01-09
If i have some values that look like this:

M000000113
M000001194
M000000245
0000018759
0000011011

I want to select these values but make this number M000000113 look like this M113 and numbers like this 0000011011 look like 11011.
Bassically get rid of leading 0s and all 0s in between the M and the number.  Any suggestions?
0
Comment
Question by:jmp02008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 6

Accepted Solution

by:
ChrisKing earned 2000 total points
ID: 17036886
1> declare @var varchar(20)
2> select @var = 'M000000113'
3> select substring(@var,1,patindex('%[0-9]%',@var)-1) + convert(varchar(20),convert(numeric(20),substring(@var,1+patindex('%[0-9]%',@var)-1,20)))
4>
5> select @var = '0000018759'
6> select substring(@var,1,patindex('%[0-9]%',@var)-1) + convert(varchar(20),convert(numeric(20),substring(@var,1+patindex('%[0-9]%',@var)-1,20)))
go
7> (1 row affected)

 ----------------------------------------
 M113

(1 row affected)
(1 row affected)

 ----------------------------------------
 18759

(1 row affected)
1>
0
 
LVL 6

Expert Comment

by:ChrisKing
ID: 17036922
1> create table #test(oldvalue varchar(20))
2> insert into #test select 'M000000113'
3> insert into #test select 'M000001194'
4> insert into #test select 'M000000245'
5> insert into #test select '0000018759'
6> insert into #test select '0000011011'
7>
8> select oldvalue
9> ,      newvalue = substring(oldvalue,1,patindex('%[0-9]%',oldvalue)-1)
10>                 + convert(varchar(20),convert(numeric(20),substring(oldvalue,1+patindex('%[0-9]%',oldvalue)-1,20)))
11> from   #test
12> go
 oldvalue             newvalue
 -------------------- ----------------------------------------
 M000000113           M113
 M000001194           M1194
 M000000245           M245
 0000018759           18759
 0000011011           11011

(5 rows affected)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
I don't pretend to be an expert at this, but I have found a few things that are useful. I hope that sharing them here will help others, so they will not have to face some rather hard choices. Since I felt this to be a topic of enough importance and…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question