LIKE-Func

 

 

NOTE - Use your Browsers BACK Button to return to prior page or CLICK here.

   LIKE-Func - Simulates Oracle LIKE Operator
pnants01.jpg (23312 bytes) This page demonstrates the use of my LIKE function which simulates the Oracle LIKE operator... My function utilizes the same wildcard (%) and position (_) parameters... An escape character can be passed to allow '%' or '_' to be treated as a character... this was another fun function to create... just like this ant picture...

You may find the LIKE operator in other databases such as DB2 and SQLBase...
the LIKE-Func routine has been specifically modeled after the Oracle version
which may behave slightly different than the LIKE operator in other databases...
You can find the LIKE-Func function in my custom SQR library TDFUNC.SQC

   LIKE-Func - Input / Output Parameters
 
Input    - Input String
Input    - Search Pattern
Input    - Escape Character
Output  - Match Indicator (Y/N)

The escape character input parameter allows you to use the wildcard or positional characters ( % and _ ) as string characters to search for... See the program source code for additional documentation... Also refer to the Oracle SQL reference for a full description of the LIKE operator...

EXAMPLES:

String               Pattern       Escape     Match
-------------------  ------------  ---------  -----
ABCDE                A%                       Y
ABCDE                A%E                      Y
ABCDE                __C%                     Y
ABCDE                _C%                      N
ABCDE                %A%B%C%D%E%              Y
ABCDE                %A%B%_%D%E%              Y
ABCDE                ABCDE_                   N
ABCDE                ABC_                     N
ABCDE                %E_                      N
ABCDE                %_E                      Y
ABCDE                %C_E                     Y
ABCDE                ABCD_                    Y
ABCDE                ___D_                    Y
ABCDE                ___D_%                   Y
ABCDE                _____%                   Y
ABCDE                ABCDE%                   Y
DeLia, Tony          %D%L% %T%                Y
50% OFF              %^% OFF       ^          Y
50 OFF               %^% OFF       ^          N
PS_PERSONAL_DATA     %_DATA        ^          Y
PS_PERSONALXDATA     %_DATA        ^          Y
PS_PERSONAL_DATA     %^_DATA       ^          Y
PS_PERSONALXDATA     %^_DATA       ^          N
PS_PERSONAL_DATA2    %^_DATA       ^          N
PS_PERSONAL_DATA2    %^_DATA%      ^          Y
-------------------  ------------  ---------  -----
                    

   LIKE-Func - Source Code
!**********************************************************************
!*       LIKE Pattern Matching (Based on Oracle LIKE Operator)        *
!**********************************************************************
!*                                                                    *
!*        INPUT: $I_string  - Input String                            *
!*               $I_pattern - Search Pattern                          *
!*               $I_escape  - Escape Character                        *
!*       OUTPUT: $O_match   - Match Indictor (Y/N)                    *
!*                                                                    *
!*               To de-activate this function:                        *
!*               #define LIKE_Func_Remove                             *
!*                                                                    *
!**********************************************************************
!*                                                                    *
!*      EXAMPLE: do LIKE-Func('ABCDE',  'A%',     '',  $O_match1)     *
!*               do LIKE-Func('ABCDE',  'A%E',    '',  $O_match2)     *
!*               do LIKE-Func('ABCDE',  '__C%',   '',  $O_match3)     *
!*               do LIKE-Func('ABCDE',  '_C%',    '',  $O_match4)     *
!*               do LIKE-Func('50% OFF','%^% OFF','^', $O_match5)     *
!*               do LIKE-Func('50 OFF', '%^% OFF','^', $O_match6)     *
!*                                                                    *
!*      RESULTS: $O_match1 = 'Y'  - Starts with 'A' rest wildcard     *
!*               $O_match2 = 'Y'  - Wildcard between 'A' and 'E'      *
!*               $O_match3 = 'Y'  - 2 fixed (any), 'C' rest wildcard. *
!*               $O_match4 = 'N'  - 'C' not in second position.       *
!*               $O_match5 = 'Y'  - Wildcard then '% OFF' (escape)    *
!*               $O_match6 = 'N'  - '%' not in string                 *
!*                                                                    *
!**********************************************************************
!*                                                                    *
!*         NOTE: This function mimics the Oracle LIKE Operator.       *
!*               Example 5 ($O_match5) would be performed in SQL*Plus *
!*               as follows:                                          *
!*                                                                    *
!*               WHERE '50% OFF' LIKE '%^% OFF' ESCAPE '^'            *
!*                                                                    *
!*               The Escape Character tells Oracle the associated     *
!*               instance of '%' is to be treated as a character      *
!*               and not a wildcard.                                  *
!*                                                                    *
!*               The LIKE operator may vary by database. For example  *
!*               SQLBase processes nulls differently than Oracle.     *
!*               Oracle always returns false if either the string or  *
!*               pattern are null. SQLBase may possibly return true   *
!*               if a null is present in either or both.              *
!*                                                                    *
!*               SQLBASE: where ''  like ''   <=== true               *
!*                ORACLE: where ''  like ''   <=== false              *
!*                                                                    *
!*               SQLBASE: where ''  like '%'  <=== true               *
!*                ORACLE: where ''  like '%'  <=== false              *
!*                                                                    *
!*               SQLBASE: where 'X' like ''   <=== false              *
!*                ORACLE: where 'X' like ''   <=== false              *
!*                                                                    *
!*               Modify to suit your particular requirements.         *
!*                                                                    *
!**********************************************************************

#ifndef LIKE_Func_Remove

begin-procedure LIKE-Func($I_string, $I_pattern, $I_escape, :$O_match)

let $O_match = 'Y'

!   Oracle Null Handling (Modify as needed)
if  isnull($I_pattern)
or  isnull($I_string)
    let $O_match = 'N'
end-if

let $special = '%_' || $I_escape
let $last    = '_'
let $dubb    = ''

let #lenS    = length($I_string)
let #lenP    = length($I_pattern)
let #posS    = 1
let #posP    = 1

let $srch    = ''

!   PHASE 1 - Matching driven within Pattern Loop

while #posP <= #lenP

   let $char = substr($I_pattern, #posP, 1)

   if  instr($special, $char, 1) > 0

       if $srch <> ''

          let #srch  = instr($I_string, $srch, #posS)

          if  #srch  = 0
              let $O_match = 'N'
          else
              if  $last  = '_'
              and #srch <> #posS
              and $dubb <> '%_'
                  let $O_match = 'N'
                  let #posP    = #lenP + 1
                  break
              else
                 let #posS = #srch + length($srch)
                 let $srch = ''
              end-if
          end-if

       end-if

       !  Special Processing Characters
       evaluate $char
          when = $I_escape
            let $esc     = 'Y'

          when = '_'
            if $esc      = 'Y'
               let $srch = $srch || $char
            else
               if  #posS <= #lenS
                   let #posS = #posS + 1
               else
                   let #posP = #lenP + 1
                   let $O_match = 'N'
                   break
               end-if
               let $dubb = $last || $char
               let $last = $char
            end-if
            let $esc     = 'N'

          when = '%'
            if $esc      = 'Y'
               let $srch = $srch || $char
            else
               let $dubb = $last || $char
               let $last = $char
            end-if
            let $esc     = 'N'
       end-evaluate
   else
       let $srch    = $srch || $char
       let $esc     = 'N'
   end-if

   let #posP = #posP + 1

end-while

!   PHASE 2 - Matching "residue" outside Pattern Loop

if $O_match = 'Y'

   if $srch <> ''

      if instr($special,$char,1) = 0

         let #srch  = instr($I_string, $srch, #posS)

         if  #srch  = 0
             let $O_match = 'N'
         else
             if  $last  = '_'
             and #srch <> #posS
             and $dubb <> '%_'
                 let $O_match = 'N'
             else
                 let #posS = #srch + length($srch)
                 if  #posS < #lenS + 1
                     let $O_match = 'N'
                 end-if
             end-if
         end-if
      end-if
   else
      if  $char  = '_'
      and #posS  < #lenS + 1
          let $O_match = 'N'
      end-if
   end-if

end-if

end-procedure

#endif
                    

   Feedback
ftoct01.jpg (12389 bytes) I would appreciate any feedback you may have on this site. Send mail to tdelia@erols.com or click on the Octopus.

   Technical difficulties?
pg001.jpg (35890 bytes) Please report any technical difficulties you may encounter to the address above OR click on the Octopus. Thanks.

NOTE - Use your Browsers BACK Button to return to prior page.

Tony DeLia - Updated March 05, 2000