Solved

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

Posted on 2013-01-08
8
1,456 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 42

Expert Comment

by:EugeneZ
ID: 38757168
if you need universal -- you can try to use regular expressions
0
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

770 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