Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

To extract first particular pattern from irregular description.

Posted on 2008-06-16
3
Medium Priority
?
210 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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 the fundamental information of how to create a table.

885 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