Solved

how do i remove numbers from data value?

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

0
Comment
Question by:Amien90
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 41

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 41

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 41

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Copy Database Wizard 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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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