May 06, 2009

SQL Functions

COUNT() - returns the number of rows of the specified column.
SELECT COUNT(column_name)
FROM table_name
WHERE expression
SELECT COUNT(DISTINCT column_name)
FROM table_name
WHERE expression

AVG() - returns the average value of a numeric column.
SUM() - returns the sum of a specific column.
MAX() - returns the largest value of a specific column.
MIN() - returns the smallest value of a specific column.
FIRST() - returns the first value of a specific column.
LAST() - returns the last value of a specific column.
Example:
SELECT AVG(column_name)
FROM table_name
WHERE expression

MID()
- Extract characters from a text field.
- start_position: the default starting position is 1.
- length: the number of characters to return (optional)
SELECT MID(column_name, start_position, length)
FROM table_name

UCASE() - converts a value to upper case.
LCASE() - converts a value to lower case.
LEN() - returns the length of a text field.
Example:
SELECT UCASE(column_name)
FROM table_name

ROUND()
- Rounds a numeric field to the number of decimals specified.
- decimal: specifies the number of decimals to be returned.
SELECT ROUND(column_name, decimal)
FROM table_name

FORMAT()
- formats how a field is to be displayed.
- format: specifies the format.
SELECT FORMAT(column_name, format)
FROM table_name
Example:
SELECT FORMAT(column_name, 'YYYY-MM-DD') AS New_Date
FROM table_name

0 comments:


Post a Comment