Solved

T-SQ: Extract Only Letters from an Alphanumeric String

Posted on 2014-01-30
3
1,931 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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