Solved

how do i remove numbers from data value?

Posted on 2009-07-15
12
210 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 300 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 100 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 17

Assisted Solution

by:pssandhu
pssandhu earned 100 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore 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.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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