September 2nd, 2008

SIMILAR TO (Matching to an Expression)

Posted by admin in D. WHERE Clause

> The SIMILAR operand was introduced by SQL:1999 and gives an alternate method of doing a comparison.
> With SIMILAR you can compare a character string to an expression.

SELECT column1, column2, ...
  FROM table1, table2, ...
 WHERE testcolumn [NOT] SIMILAR TO (expression), ...;

Example:

WHERE DAY_FIELD SIMILAR TO
      '('Day=' (Mon | Tue | Wed | Thu | Fri))'

or

WHERE COLOR SIMILAR TO
      '('The color is ' (red | green | blue | 12 | 14))'
September 1st, 2008

LIKE (Matching to a Pattern), Part 3 of 3

Posted by admin in D. WHERE Clause

> Sometimes you may wish to include an underscore or a percent sign in the LIKE comparison pattern.
> You need a way to tell SQL that the underscore or percent sign is not to be used as a wildcard character.
> You can do this by typing an escape character just prior to the character you wish SQL to take literally.
> You can choose any character as the escape character, as long as that character does not appear in the string you are testing, as shown:

> This statement will look for the percent sign (%) anywhere in the NOTES.
> The first % says there may be any number of characters ahead of the percent sign.
> The next #% says that it is looking for the percent sign and it is not a wildcard character because it has the escape character ahead of it. The escape character must be defined as shown above.
> The last % says that there may be any number of characters after the percent sign.

> Access does not support the ESCAPE clause. Instead it uses brackets [ ] for an exact character matches on a single chracter. In Access WHERE NOTES LIKE ‘%[%]%’ does the same as the example shown above.
> SQL Server expands on the Microsoft use of brackets. A range of a single character match can be specified and the [^] character means not. Examples:

‘[a-g]et’ matches ‘bet’, & ‘get’, but not ‘let’.
‘[bln]et’ matches ‘bet’, ‘let’, & ‘net’, but not ‘get’.
‘ma[^t]%’ matches ‘man’, & ‘maverick’, but not ‘mat’, or ‘material’.

August 29th, 2008

LIKE (Matching to a Pattern), Part 2 of 3

Posted by admin in D. WHERE Clause

« Previous PageNext Page »