?
Solved

how do i remove numbers from data value?

Posted on 2009-07-15
12
Medium Priority
?
212 Views
Last Modified: 2012-08-13
't1e2s3t' --> 'test'

0
Comment
Question by:Amien90
[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
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 42

Accepted Solution

by:
pcelba earned 1200 total points
ID: 24864916
SELECT dbo.RemoveDigits('a2s3dff4fdf556gjgh098')  

returns

asdfffdfgjgh


CREATE FUNCTION dbo.RemoveDigits(@MyText varchar(4000))
RETURNS varchar(4000)
AS
BEGIN
 
RETURN replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(@MyText,'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''),'0','')
 
END

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 24865962
this will do.
declare @ActualVariable nvarchar(1000) = 't1e2s3t'
DECLARE @ModifiedVariable nvarchar(1000) = ''
 SELECT @ModifiedVariable = @ModifiedVariable + CASE WHEN number LIKE '[^0-9]' THEN number ELSE '' END 
   FROM (SELECT SUBSTRING(@ActualVariable,number,1) AS number 
           FROM (SELECT number FROM master..spt_values 
                  WHERE type = 'p' AND number BETWEEN 1 AND LEN(@ActualVariable)) as t) as t 
 select replace(replace(@ModifiedVariable,'(',''),')','')

Open in new window

0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 400 total points
ID: 24865972
you can create a function with code provided and use that function.

select dbo.fnOnlyString('t1e2s3t')

create function dbo.fnOnlyString(@accountname nvarchar(1000)) returns nvarchar(1000) as
begin
DECLARE @account_name nvarchar(1000)
    SET @account_name=''
 SELECT @account_name = @account_name + CASE WHEN number LIKE '[^0-9]' THEN number ELSE '' END 
   FROM (SELECT SUBSTRING(@accountname,number,1) AS number 
           FROM (SELECT number FROM master..spt_values 
                  WHERE type = 'p' AND number BETWEEN 1 AND LEN(@accountname)) as t) as t 
 return replace(replace(@account_name,'(',''),')','')
end

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 17

Assisted Solution

by:pssandhu
pssandhu earned 400 total points
ID: 24866255
Oh well I'll pitch in with 2 cents too. Only difference is I am using PATINDEX:
-- FUNCTION CALL
Select dbo.fn_GetString('t45es954t')
 
-- CREATE FUNCTION
CREATE FUNCTION fn_GetString (@Numstr nvarchar(max))
   RETURNS nvarchar(max)
AS
BEGIN
WHILE PATINDEX('%[^A-Z]%',@NumStr)> 0
SET @NumStr = REPLACE(@NumStr,SUBSTRING(@NumStr,PATINDEX('%[^A-Z]%',@NumStr),1),'')
RETURN @NumStr 
END

Open in new window

0
 
LVL 42

Expert Comment

by:pcelba
ID: 24868012
I have a few notes to other solutions:
Sharath: Your approach is very interesting but very slow and it works for strings up to 2047 characters only (your function removes parenthesis which is not requested)
pssandhu: Removing digits does not mean to leave just letters.

The "nested REPLACEs" approach looks stupid but it is easy to understand and very fast.

BTW, to implement this in Visual FoxPro is a peanut because the function is built-in already:

   CHRTRAN('s23d3fc456sX','1234567890','')  returns  'sdfcsX'

Now when the Visual FoxPro development team is working on SQL Server we may expect some useful function implemented soon...
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24869044
pcbella: Good point! There might be apostrophies or quotes that might be part of the string that needs to be retained as well. In that case I would do it this way; it's similar to your approach but instead of using nested Replace, I use a loop :-)
Cheers!

CREATE FUNCTION fn_GetString (@Numstr nvarchar(max))
   RETURNS nvarchar(max)
AS
 
BEGIN
 
DECLARE @Counter INT
SET @Counter = 0
 
WHILE @Counter <= 10
 BEGIN
	SET @NumStr = REPLACE(@NumStr,@Counter,'')
	SET @Counter = @Counter+1
 END
 
RETURN @NumStr 
 
END

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 24875597
tested my solution and its working for upto 2047 characters. And its not even taking 1 second for execution. I dont think its extremely slow. but agree with you on the length of the string.
Amien90, - do you have strings with 2000+ characters?  
0
 

Author Comment

by:Amien90
ID: 24889349
nope. . never 2000+ chars ... i took a function for this problem .. thanks all..

(strange that SQL doesnt have a standard option for this!)
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24890423
may i know the reason for 'B'?
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24893629
I agree, it should be "C"  :-)))

May I know the reason of accepting late, not fully functional, and slow solution ??  

What's wrong with dbo.RemoveDigits finction from answer ID:24864916 ?

:-)))
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

765 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