btamulis 1/28/2022 11:56:59 AM

SQL Sub String question

I have a requirement to extrapolate from a SQL String Field our Invoice Number.

My Query is as follows:

SELECT CUSTNMBR,
       CAST(DATE1 AS DATE) AS Date,
       Action_Promised,
       Amount_Promised,
       SUBSTRING(
                    Note_Display_String,
                    PATINDEX('%REF%', Note_Display_String) + 0,
                    LEN(Note_Display_String) - PATINDEX('%;REF;%', Note_Display_String) + 0
                ) AS InvNumber,
       Note_Display_String
    FROM CN00100
    WHERE DATE1 >= '2022-01-01'
          AND Action_Promised LIKE '%Dun%';
 

I need to get the Invoice Number (REFXXXXXX) from the Note_Display_String Field. How can I change my Substring to only Grab REFXXXXXX no matter where in the string it falls? Thanks in advance......

Query returns this:

Version: All
Section: SQL Scripts


Table Definition Quick Links
All Tables
SOP Tables
RM Tables
GL Tables
POP Tables
HR Tables
PM Tables
UPR Tables
IV Tables
Olympic Tables
3