SQL Tutorial
|
|
|
|
| Articles Reviews Structured Query Language | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Written by Bogdan V | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Thursday, 14 September 2006 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Page 11 of 17 {mos_sb_discuss:29}
The syntax is as follows: Let's assume that we have the following two tables, Table Store_Information
Table Internet_Sales
and we want to find out all the dates where there are both store sales and internet sales. To do so, we use the following SQL statement:
SELECT Date FROM Store_Information Result:
Please note that the INTERSECT command will only return distinct values. SQL MINUS The MINUS operates on two SQL statements. It takes all the results from the first SQL statement, and then subtract out the ones that are present in the second SQL statement to get the final answer. If the second SQL statement includes results not present in the first SQL statement, such results are ignored. The syntax is as follows:
[SQL Statement 1] Let's continue with the same example: Table Store_Information
Table Internet_Sales
and we want to find out all the dates where there are store sales, but no internet sales. To do so, we use the following SQL statement:
SELECT Date FROM Store_Information Result:
"Jan-05-1999", "Jan-07-1999", and "Jan-08-1999" are the distinct values
returned from "SELECT Date FROM Store_Information." "Jan-07-1999" is
also returned from the second SQL statement, "SELECT Date FROM
Internet_Sales," so it is excluded from the final result set.
· MySQL: CONCAT()
The syntax for CONCAT() is as follows:
CONCAT(str1, str2, str3, ...): Concatenate str1, str2, str3, and any
other strings together. Please note the Oracle CONCAT() function only
allows two arguments -- only two strings can be put together at a time
using this function. However, it is possible to concatenate more than
two strings at a time in Oracle using '||'.
Let's look at some examples. Assume we have the following table: Table Geography
Example 1:
MySQL/Oracle: Result: 'EastBoston'
Example 2:
Oracle:
Result: 'East Boston'
Example 3:
SQL Server:
Result: 'East Boston' SQL SUBSTRING
· MySQL: SUBSTR(), SUBSTRING()
The most frequent uses are as follows (we will use SUBSTR() here):
SUBSTR(str,pos): Select all characters from <str> starting with
position <pos>. Note that this syntax is not supported in SQL
Server.
SUBSTR(str,pos,len): Starting with the <pos>th character in
string <str> and select the next <len> characters.
Assume we have the following table: Table Geography
Example 1:
SELECT SUBSTR(store_name, 3)
Result:
's Angeles'
Example 2:
SELECT SUBSTR(store_name,2,4)
Result: 'an D'
· MySQL: TRIM(), RTRIM(), LTRIM()
The syntax for these trim functions are:
TRIM([[LOCATION] [remstr] FROM ] str): [LOCATION] can be either
LEADING, TRAILING, or BOTH. This function gets rid of the [remstr]
pattern from either the beginning of the string or the end of the
string, or both. If no [remstr] is specified, white spaces are removed.
LTRIM(str): Removes all white spaces from the beginning of the string.
RTRIM(str): Removes all white spaces at the end of the string.
Example 1:
SELECT TRIM(' Sample ');
Result:
'Sample'
Example 2:
SELECT LTRIM(' Sample ');
Result:
'Sample '
Example 3:
SELECT RTRIM(' Sample ');
Result:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Last Updated ( Saturday, 30 June 2007 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| < Prev |
|---|







