0

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
3
  • what do you want to do with this? Commented Jul 28, 2016 at 3:00
  • I have to pick the bill id's from a string. Commented Jul 28, 2016 at 3:02
  • i dont even saw any problem with your code.. its working fine and return the billid.. Commented Jul 28, 2016 at 3:06

2 Answers 2

1

With the help of a Parser Function.

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'

-- Clean and Normalize String
Set @Str = Replace(Replace(Replace(@Str,'Bill id','BILLID'),'BILLID :','BILLID:'),'BILLID: ','BILLID:')
Select Distinct BillID=Replace(Key_Value,'BILLID:','') 
 From  [dbo].[udf-Str-Parse](@Str,' ') 
 Where Key_Value Like 'BILLID%'

Returns

BillID
AS12345
WE23456

The UDF

ALTER FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimeter varchar(10))
--Usage: Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--       Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--       Select * from [dbo].[udf-Str-Parse]('id26,id46|id658,id967','|')
--       Select * from [dbo].[udf-Str-Parse]('hello world. It. is. . raining.today','.')

    Returns @ReturnTable Table (Key_PS int IDENTITY(1,1), Key_Value varchar(max))
    As
    Begin
       Declare @XML xml;Set @XML = Cast('<x>' + Replace(@String,@Delimeter,'</x><x>')+'</x>' as XML)
       Insert Into @ReturnTable Select Key_Value = ltrim(rtrim(String.value('.', 'varchar(max)'))) FROM @XML.nodes('x') as T(String)
       Return 
    End
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you John, I thought system functions such as Stuff can do parsing
@goofyui I'll noodle it a bit more, Perhaps there is a more elegant way
@goofyui Full-Disclosure. Your process revealed a bug in my oringnal parser. When two consecutive IDENTICAL strings , only one would be returned. Since you only wanted distinct, it wouldn't matter for this test, HOWEVER, the parser should be correct as a foundation helper. The UPDATED function listed above has the SAME FUNCTIONALITY and should be updated on your system.
@goofyui I should also add, it is faster as well.
0

I think a custom function using the regular expression id:\s*[A-Z][A-Z][0-9]+.

1 Comment

keyword to filter the statement is bill id:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.