Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

how do i remove numbers from data value?

't1e2s3t' --> 'test'

0
Amien90
Asked:
Amien90
  • 4
  • 3
  • 2
  • +1
3 Solutions
 
pcelbaCommented:
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
 
SharathData EngineerCommented:
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
 
SharathData EngineerCommented:
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
pssandhuCommented:
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
 
pcelbaCommented:
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
 
pssandhuCommented:
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
 
SharathData EngineerCommented:
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
 
Amien90Author Commented:
nope. . never 2000+ chars ... i took a function for this problem .. thanks all..

(strange that SQL doesnt have a standard option for this!)
0
 
SharathData EngineerCommented:
may i know the reason for 'B'?
0
 
pcelbaCommented:
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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now