Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Extract numbers or letters from mixed string

Author  Topic 

kelleyb
Yak Posting Veteran

61 Posts

Posted - 2004-03-16 : 18:56:18
Yeah, it's pretty simple. Maybe it'll help someone out.


-- USAGE: fn_extract_chars(string_to_search, 'letters' -or- 'numbers')
CREATE FUNCTION fn_extract_chars (@x varchar(128), @y char(7))
RETURNS varchar(128)
AS
BEGIN
DECLARE @chars varchar(128)
DECLARE @pos int
DECLARE @action varchar(32)
SET @pos = 0
SET @chars = ''

IF @y = 'numbers' SET @action = '[0-9]'
ELSE IF @y = 'letters' SET @action = '[a-zA-Z]'

WHILE @pos < (DATALENGTH(@x) + 1)
BEGIN
IF PATINDEX(@action,SUBSTRING(@x, @pos, 1)) > 0
BEGIN
SET @chars = @chars + (SELECT SUBSTRING(@x, @pos, 1))
END
SET @pos = @pos + 1
END
RETURN(@chars)
END




massspectrometry
Yak Posting Veteran

50 Posts

Posted - 2007-07-12 : 00:15:59
can you teach me how to use it? I'm using sql server 2000... and i run everything in query analyzer
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 00:39:35
quote:
Originally posted by massspectrometry

can you teach me how to use it? I'm using sql server 2000... and i run everything in query analyzer


[CODE]
SELECT
DBO.fn_extract_chars('ASDHJ98','NUMBERS')
[/CODE]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-12 : 02:05:10
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=79083


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-12 : 05:18:55
Another one

Select
dbo.GetCharacters('Pk9kjsdfrm657sd19471' ,'0123456789')

from
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56713

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -