Solved

SQL String parsing and fetching certain value

Posted on 2016-07-27
5
49 Views
Last Modified: 2016-07-30
I have a string which i have to parse and retrieve the bill id fields. My string can have either one bill id or multiple bill id's.



Declare @str nvarchar(1000)

set @Str = 'Process of submitting the bill id: AS12345 is send back to Customer. Bill id:WE23456 and Bill id: AS12345 came from customers'

Declare @t table (billid varchar(100))
Insert into @t values ('AS12345'),('WE23456'),('AS12345')

select distinct billid from @t

Open in new window

0
Comment
Question by:chokka
5 Comments
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 41732353
not the prettiest but here goes:
Declare @str nvarchar(1000)

set @Str = 'Process of submitting the bill id: AS12345 is send back to Customer. Bill id:WE23456 and Bill id: AS12345 came from customers'

Declare @t table (billid varchar(100))
Insert into @t values ('AS12345'),('WE23456'),('AS12345')

select distinct billid from @t;

WITH cte AS (
SELECT @str base, PATINDEX('%[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%',@str) start, SUBSTRING(@str,PATINDEX('%[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%',@str),7) BillId, SUBSTRING(@str,PATINDEX('%[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%',@str)+7,LEN(@str)) remaining
UNION ALL
SELECT cte.remaining base, PATINDEX('%[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%',cte.remaining) start, SUBSTRING(cte.remaining,PATINDEX('%[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%',cte.remaining),7) BillId, SUBSTRING(cte.remaining,PATINDEX('%[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%',cte.remaining)+7,LEN(cte.remaining)) remaining
FROM cte
WHERE PATINDEX('%[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9]%',cte.remaining) <> 0
)
SELECT DISTINCT cte.BillId
FROM cte

Open in new window

0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41732392
Chris is trying to etract all parts of @Str resembling a bill id pattern. That's fine, what this doesn't do is compare with really existing bill ids.

There's a simple solution without recursive cte, but it'll have a performance disadvantage with a large bill table:

Select bill.billid from @t as bill 
where @Str Like '%'+bill.billid+'%'

Open in new window


You'd better be able to reduce the amount of bill ids to check, by having a flag of open bills you want to check and skip all the bills already closed for weeks, months, even years.

Select bill.billid from @t as bill
where bill.IsOpen AND @Str Like '%'+bill.billid+'%'

Open in new window


This solution is checking every reevant)billid, whether being part of @Str or not.

Tihs could combined, eg you could take Chris resut and inner join it with bills to see what he fishes really are bills and not any other ZZ99999 pattern. His solution also lacks finding any bill ids with more or less digits.

Bye, Olaf.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 41732414
you can try this. Assumption is you always have : before the billid.
Declare @str nvarchar(1000)

set @Str = 'Process of submitting the bill id: AS12345 is send back to Customer. Bill id:WE23456 and Bill id: AS12345 came from customers'

Declare @t table (billid varchar(100))
Insert into @t values ('AS12345'),('WE23456'),('AS12345')

;WITH CTE AS (
SELECT RTRIM(LEFT(str1,CHARINDEX(' ',str1)))  billid FROM (
SELECT ltrim(SUBSTRING(str1, n, CHARINDEX(':', str1 + ':',n) - n)) AS str1
 FROM (SELECT @Str str1) t1
CROSS JOIN (SELECT number FROM master..spt_values WHERE type = 'P')  AS Numbers(n)
WHERE SUBSTRING(':' + str1, n, 1) = ':'
  AND n < LEN(str1) + 1) t2)
SELECT DISTINCT t1.billid
  FROM CTE c1
  JOIN @t t1 ON c1.billid = t1.billid

Open in new window

0
 

Author Closing Comment

by:chokka
ID: 41735344
I am always thankful to you. A lot to learn and learning from you !!
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 41736118
thanks, glad you are learning things.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

825 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