Solved

how do i remove numbers from data value?

Posted on 2009-07-15
12
209 Views
Last Modified: 2012-08-13
't1e2s3t' --> 'test'

0
Comment
Question by:Amien90
  • 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 40

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 40

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 40

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 40

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

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 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