Advertisement
rfv123

32006484/ - SQL `LIKE` tests - let me count the ways...

Aug 14th, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.72 KB | None | 0 0
  1. SET @lPC               = '%';                       /* percent sign character */
  2. SET @lBS               = '\\';                      /* one backslash when selected and in LIKE test */
  3. SET @lBS2              = CONCAT(@lBS, @lBS);        /* two backslash */
  4. SET @lLikePCLit        = '\%';                   /* LIKE test with no escape gives a literal percent character */
  5.  
  6. SET @LikePCPCLit       = CONCAT('%', @lLikePCLit);      /* LIKE test - wildcard - anything ending with a literal percent sign */
  7. SET @ALikeAPCWild      = 'a%';                          /* LIKE Test - anything starting with an 'a' */
  8. SET @LikeAPCB          = CONCAT('a', @lPC, 'b');        /* LIKE Test - anything starting 'a' and ending 'b' */
  9. SET @LikeABSLitBNoEsc  = CONCAT('a', @lBS, @lBS, 'b');  /* LIKE test - exact match of 'a\b' */
  10. SET @LikeABSLitBEsc    = CONCAT('a', @lBS, 'b');        /* LIKE test - exact match of 'a\b' escaped */
  11. SET @LikeAPCLitBNoEsc  = CONCAT('a', @lLikePCLit, 'b'); /* LIKE test - exact match of 'a%b' - no wildcard! */
  12. SET @LikeAPCLitBEsc    = CONCAT('a', @lPC, 'b');        /* LIKE test - exact match of 'a%b' - escaped! */
  13.  
  14. SET @targetAQB         = 'aqb';          /* 'aqb' - an ordinary string to match */
  15. SET @targetAPCB        = 'a%b';          /* 'a%b' - a literal percent sign in a string to match - fun */
  16. SET @targetABSB        = 'a\\b';         /* 'a\b' - as literal backslash in a string to match  - fun */
  17.  
  18. SELECT @lBS AS atOneBackSlash,  
  19.        @lBS2 AS atTwoBackSlash,
  20.        @LikePCLit AS atLikePCLit,
  21.        @targetAQB AS atAQB,
  22.        @targetAPCB AS atApcB,
  23.        @targetABSB AS atAbsB,
  24.        /* Now let us start on the `LIKE` tests */
  25.        /* */
  26.        /* Wildcard matches */
  27.        @targetAQB   LIKE @LikeAPCB AS `aqb LIKE a% - True`,
  28.        @targetAPCB  LIKE @LikeAPCB AS `a%b LIKE a% - True`,
  29.        @targetABSB  LIKE @likeAPCB AS `a\b LIKE a% - True`,
  30.        /* */
  31.        /* Unescaped Exact matches */
  32.        @targetAQB   LIKE 'aqb'        AS `UnEsc: aqb LIKE aqb - True`,
  33.        @targetAPCB  LIKE @LikeAPCLitBNoEsc AS `UnEsc:  a%b LIKE a\%b - True`,
  34.        @targetABSB  LIKE @likeABSLitBNoEsc AS `UnEsc: a\b LIKE a\\\\b - True`,
  35.        /* Not Escaped Exact matches */
  36.        @targetAQB   LIKE 'aqb'            AS `UnEsc: aqb LIKE aqb  - True`,
  37.        @targetAPCB  LIKE @LikeAPCLitBEsc  AS `UnEsc:  a%b LIKE a%b - True - as wildcard!`,
  38.        @targetABSB  LIKE @likeABSLitBEsc  AS `UnEsc: a\b LIKE a\\b - False`,
  39.        /* Escaped Exact matches */
  40.        @targetAQB   LIKE 'aqb'            ESCAPE '!' AS `Esc: aqb LIKE aqb - True`,
  41.        @targetAPCB  LIKE @LikeAPCLitBEsc  ESCAPE '!' AS `Esc:  a%b LIKE a%b - True`,
  42.        @targetABSB  LIKE @likeABSLitBEsc  ESCAPE '!' AS `Esc: a\b LIKE a\\b - True`,
  43.        '';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement