Solved

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

Posted on 2013-01-08
8
1,386 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PL/SQL query 14 50
Shutdown server relation to performance at application on early use 6 59
Sql query 34 20
Stored procedure 23 9
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

758 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

19 Experts available now in Live!

Get 1:1 Help Now