ThreadAbort
Calling this method terminates the thread...

Function to remove letters from a string in SQL Server

Tuesday, March 17, 2009 8:39 PM

 I found a function at
http://www.softstuff-consulting.com/kbase/showkb.asp?id=56

that removes letters from a varchar in SQL Server.  I found it to be a bit slow for my needs.  So, I made the following function that performs better for me.

alter function NoLetters(@string2number varchar(200)) returns varchar(200)
as
begin
 declare @c int
 declare @num varchar(200)
 set @num = ''
 declare @txtCurrent char(1)
 set @c=1
 while @c<=len(@string2number)
 begin
  set @txtCurrent = substring(@string2number,@c,1)
  if ascii(@txtCurrent) between 48 and 57
   set @num = @num + @txtCurrent

  set @c=@c+1
 end
return @num
end

To use the function it can be added to a print statement like
PRINT dbo.NoLetters('ABC1234')
This will return 1234

 


Feedback

# re: Function to remove letters from a string in SQL Server

I've used this kind of tricks before. I hope its forgiving. 2/19/2010 1:24 PM | frank

Post a comment





 

Please add 7 and 3 and type the answer here: