Solved

To extract first particular pattern from irregular description.

Posted on 2008-06-16
3
184 Views
Last Modified: 2008-07-06
SQL Server 2000

The function would be like
f_invoice_no ( @string varchar(1000)

@string
---------
Amount realzied dbldin & Sons $00444/07 9250 @60.79
Amount realzied dbldin & Sons $00444/07 9250 @60.79
Amount realzied dbldin & Sons $00444/07 9250 @60.79
Amount realzied dbldin & Sons $00444/07 9250 @60.79
Amount realzied dbldin & Sons $00444/07 9250 @60.79
Dc Purchased kmency garments inv#00507/07 #8752.37 @59.30
Dc Purchased kmency garments inv#00507/07 #8752.37 @59.30
Dc Purchased kmency garments inv#00507/07 #8752.37 @59.30
Dc Purchased kmency garments inv#00507/07 #8752.37 @59.30
Amount Realzied abcdef Ready made garments Inv#00536/07
Amount Realzied abcdef Ready made garments Inv#00536/07
Amount Realzied abcdef Ready made garments Inv#00536/07
AMount realzied xklo Corporation inv#00538/07, 00539/07
AMount realzied xklo Corporation inv#00538/07, 00539/07
AMount realzied xklo Corporation inv#00538/07, 00539/07
Dc Purchased xklo Corporation $101045.06 @60.62 inv#00188/08
Dc Purchased xklo Corporation $101045.06 @60.62 inv#00188/08
Dc Purchased xklo Corporation $101045.06 @60.62 inv#00188/08
Dc Purchased xklo Corporation $101045.06 @60.62 inv#00188/08
Dc Purchased xklo Corporation $101045.06 @60.62 inv#00188/08
Dc Purchased xklo Corporation $101045.06 @60.62 inv#00188/08

I want to extract invoice number of the pattern
[0-9][0-9][0-9][0-9][0-9][/][0-9][0-9]

In my sample data, for
Amount realzied dbldin & Sons $00444/07 9250 @60.79
I want 00444/07

If there are more than one available patterns , such as in my data,
AMount realzied xklo Corporation inv#00538/07, 00539/07
There are two 00538/07, 00539/07

Then
1) return all patttern          
  [00538/07,00539/07]
or return the exact text [MULTIPLE INVOICE]
whichever is easier for the expert, but it must not return only one pattern.



MTN:




0
Comment
Question by:Mehram
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 21791369
0
 

Author Comment

by:Mehram
ID: 21791448
http://www.sqlteam.com/article/regular-expressions-in-t-sql
The above is only telling whether pattern is found or not found, whereas I want the pattern to be returned.
0
 

Author Comment

by:Mehram
ID: 21792497
I have reached so far

alter function f_FirstInvNoInVdDesc (
@string varchar(1000)
) returns varchar(1000)
AS
BEGIN

    DECLARE @newstring varchar(1000)
    declare @num int
    select @newstring = ''
    select @num = 1
   
    while @num < len(@string)+1
    begin
        if Substring(@string, @num, 8) like '[0-9][0-9][0-9][0-9][0-9][/][0-9][0-9]'
         or Substring(@string, @num, 8) like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
         begin
            set @newstring = @newstring + Substring(@string, @num, 8)
            set @num = 99999
         end
   
    set @num = @num + 1
    end
    RETURN @newstring
END


It is returning the first available pattern.
This is ok, if there is only one pattern,
but, this wrong when there are more than one pattern

select dbo.f_FirstInvNoInVdDesc ('Amount realzied dbldin & Sons $00444/07 9250 @60.79')
returns
00444/07   -- this is correct

select dbo.f_FirstInvNoInVdDesc ('AMount realzied xklo Corporation inv#00538/07, 00539/07')
returns
00538/07  -- this is incorrect because there is one more pattern 00539/07
In this case , I want this to return
'MULTIPLEINV'









0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

747 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

12 Experts available now in Live!

Get 1:1 Help Now