Solved

extract 5-digits substring from string in T-SQL / pattern matching

Posted on 2013-01-08
8
1,420 Views
Last Modified: 2013-01-08
Dear Experts,

I wonder if such text operations / pattern matching are easily available in T-SQL.

select fivedigits(text_field) from my table.

fivedigits is the magic function I'm looking for.

Sample input and output:
Input "504-A34BC-322-2232" output: null
Input "504-A34BC-22325" output: 22325

Input string will always contain only one 5-digits substring.

thanks
Jarek
0
Comment
Question by:ja-rek
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38757049
Sounds like you need a regular expression match for 5 digits.

From what I'm reading (haven't had to do this myself), you need to use CLR integration to make it work.

Read these three articles:
http://www.codeproject.com/Articles/42764/Regular-Expressions-in-MS-SQL-Server-2005-2008
http://stackoverflow.com/questions/1964124/regular-expression-inside-sql-server
http://msdn.microsoft.com/en-us/magazine/cc163473.aspx

Looks like your regular expression match for 5 digits would be \d{5}   (but I'm not exactly an expert with regexps)
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 38757071
try

Declare @str varchar(50)

set @str ='504-A34BC-22325'

select case when  isnumeric (replace(right(@str,5),'-','a'))=1 then right(@str,5)
else NULL end reslt

Open in new window

0
 
LVL 1

Author Comment

by:ja-rek
ID: 38757084
sjwales: thanks, I will read these articles if I don't get ready solution
EugeneZ: sorry, this is not universal enough, I need regular expressions
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 38757168
if you need universal -- you can try to use regular expressions
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 300 total points
ID: 38757377
This would do it for your specific example:
SELECT  YourColumnName
FROM    YourTableName
WHERE   PATINDEX('%[0-9][0-9][0-9][0-9][0-9]%', YourColumnName) > 0

Open in new window

0
 
LVL 39

Assisted Solution

by:appari
appari earned 200 total points
ID: 38757425
create a UDF and use it as follows:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create FUNCTION GetFiveDigitSubString 
(
	@src varchar(50)
)
RETURNS varchar(5)
AS
BEGIN
	DECLARE @retVal varchar(5)
	declare @srcLen int
    declare @curPos int

    if @src is null 
          return null

	select @srcLen = datalength(@src), @curPos = 1, @retVal=''
    While @curPos <= @srcLen
	begin
		if substring(@src,@curpos,1) like '[0-9]'
			select @retVal=@retVal + substring(@src,@curpos,1)
		else
			select @retVal=''
		if datalength(@retVal)=5
			return @retVal
		Select @curPos = @curPos + 1
	end
		if datalength(@retVal)=5
			return @retVal
		--else 
			return null
END
GO

Open in new window


use it as follows:
select dbo.GetFiveDigitSubString(colName) from tableName
0
 
LVL 1

Author Closing Comment

by:ja-rek
ID: 38757436
Many thanks for help!
0
 
LVL 39

Expert Comment

by:appari
ID: 38757451
I was thinking too much, we can get the result by using patindex and substring functions as suggested by acperkins

select
case when patindex('%[0-9][0-9][0-9][0-9][0-9]%', colName) > 0
then substring(colName,patindex('%[0-9][0-9][0-9][0-9][0-9]%', col1),5) else null end
from tablename
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now