August 28th, 2008

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

Posted by admin in D. WHERE Clause

> The LIKE operator is used to compare a value to other similar values using wildcard characters.
> This allows you to retrieve information when only part of the value is known.
> There are two wildcards used with the LIKE operator:% – The percent sign is used to ignore zero, one, or more characters or numbers.

_ – The underscore is used to ignore one character or number.

SELECT column1, column2, …
FROM table1, table2, …
WHERE testcolumn [NOT] like ‘pattern’ [ESCAPE 'escapechar'], …;

> SQL standards for LIKE works only with character strings, not with numbers or datetimes. A few rare RDBMS systems let you use LIKE to search numeric and datetime columns.
> LIKE uses a pattern of exact characters and wildcard characters to do the match.
> Wildcards are special characters used to ignore parts of a value.
> String comparisons may be case sensitive or case insensitive depending on your RDBMS.
> Some RDBMS systems take trailing spaces into account for the LIKE operator.
> You can combine LIKE conditions with other conditions using AND and OR.
> You can reverse the LIKE test with NOT LIKE.
> Wildcard searches are slow. Do not use the LIKE command if another search will do. Do not use WHERE STATE LIKE ‘TX’ when WHERE STATE = ‘TX’ would be faster.
> NOT LIKE works the same as LIKE <>.
> Access uses a question mark (?) instead of the conventional underscore (_).

August 27th, 2008

BETWEEN (Matching to a Range)

Posted by admin in D. WHERE Clause

BETWEEN determines if a test column value falls within a specified range.

SELECT column1, column2, ...
  FROM table1, table2, ...
 WHERE testcolumn [NOT] BETWEEN lowvalue AND highvalue, ...;

> BETWEEN is the same as saying WHERE (testcolumn >= lowvalue) AND (testvalue <= highvalue).
> BETWEEN works with numbers, datetimes, character strings and expressions.
> Character string comparisons are case sensitive or insensitive depending on your DBMS.
> The first value must be lower than (or equal to) the second value separated by the word AND.
> The low value and the high value are included in the search results.
> You can reverse the selected values with NOT BETWEEN.
> You can combine BETWEEN with other conditions using AND and OR.
> The testcolumn does not have to be one of the columns you SELECT.
> If the testcolumn value is a NULL and meets the NOT BETWEEN, then the returned value is NULL.

Note: Access surrounds a date with the # symbol, such as #2050-01-01#.
DB2 and SQL Server surround the date with the single quote symbol (‘), and they omit the DATE key word. Example: ’2050-01-01′ instead of DATE ’2050-01-01′.

August 26th, 2008

IS NULL (Unknown Value)

Posted by admin in D. WHERE Clause

> NULLs represent missing or unknown values.
> A NULL does not match any value, not even other NULLs.
> “= NULL” can not be used because unknown values do not satisfy specific conditions.The RDBMS engine may try to convert NULL to a string and look for the letters N-U-L-L.
SQL uses “IS NULL” to determine a null condition.

SELECT column1, column2, ...
  FROM table1, table2, ...
 WHERE testcolumn IS [NOT] NULL, ...;

> IS NULL works for columns of any data type.
> You can find a non-NULL condition with IS NOT NULL.
> You can combine IS NULL with other conditions using AND and OR.
> A NULL is not the same thing as an empty string (”).
> Oracle is the exception because it treats an empty sting (”) as a null.
> You can forbid nulls in a table with NOT NULL in the TABLE definition. See Data Definition Language (DDL).

« Previous PageNext Page »