Solved

T-SQ: Extract Only Letters from an Alphanumeric String

Posted on 2014-01-30
3
1,954 Views
Last Modified: 2014-01-31
Hello:

I have a field called UOMSCHDL that contains alphanumeric characters.  The following represents examples of data returned for this field:  2YD, LB, EA, EA2, PKG, RL, CS0.

I want to return just the alpha and not the numeric.  Is there a simple means of extracting just the letters from a string containing both letters and numbers?

Thanks!

TBSupport
0
Comment
Question by:TBSupport
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39821006
check this out

DECLARE @T VARCHAR(1000)
DECLARE @AlphaWithCommas VARCHAR(1000)
DECLARE @AlphaWithOutCommas VARCHAR(1000)
SET @T = '2YD, LB, EA, EA2, PKG, RL, CS0'
SELECT @AlphaWithCommas = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@T,'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''),'0','')
select @AlphaWithCommas
--Remove Commas as well
select @AlphaWithOutCommas = REPLACE(@AlphaWithCommas,',','')
select @AlphaWithOutCommas

Open in new window

0
 
LVL 1

Author Comment

by:TBSupport
ID: 39822525
Thanks, Surendra!  

But, there's not a way of doing this without having to create a program declaring variables?

TBSupport
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 500 total points
ID: 39822539
I just declared variables here for convinience and to show that it works.

You can just take the complete replace statement below and apply it on a column or a variable ....

REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(<your Column or Variable>,'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''),'0','')

Open in new window


but if you are looking for some function from microsoft, then I dont there exists one for this purpose.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

840 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