General Function
General Function
General Function
1. AVG() - Returns the average value The AVG() Function The AVG() function returns the average value of a numeric column. SQL AVG() Syntax SELECT AVG(column_name) FROM table_name 2. COUNT() - Returns the number of rows The COUNT() function returns the number of rows that matches a specified criteria. SQL COUNT(column_name) Syntax The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: SELECT COUNT(column_name) FROM table_name SQL COUNT(*) Syntax The COUNT(*) function returns the number of records in a table: SELECT COUNT(*) FROM table_name SQL COUNT(DISTINCT column_name) Syntax The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column: SELECT COUNT(DISTINCT column_name) FROM table_name Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.
3. FIRST() - Returns the first value The FIRST() Function The FIRST() function returns the first value of the selected column. SQL FIRST() Syntax SELECT FIRST(column_name) FROM table_name
4. LAST() - Returns the last value The LAST() Function The LAST() function returns the last value of the selected column. SQL LAST() Syntax SELECT LAST(column_name) FROM table_name
5. MAX() - Returns the largest value The MAX() Function The MAX() function returns the largest value of the selected column. SQL MAX() Syntax SELECT MAX(column_name) FROM table_name
6. MIN() - Returns the smallest value The MIN() Function The MIN() function returns the smallest value of the selected column. SQL MIN() Syntax SELECT MIN(column_name) FROM table_name
7. SUM() - Returns the sum The SUM() Function The SUM() function returns the total sum of a numeric column. SQL SUM() Syntax SELECT SUM(column_name) FROM table_name
Scalar Functions
1. UCASE() - Converts a field to upper case The UCASE() Function The UCASE() function converts the value of a field to uppercase. SQL UCASE() Syntax SELECT UCASE(column_name) FROM table_name Syntax for SQL Server SELECT UPPER(column_name) FROM table_name
2. LCASE() - Converts a field to lower case The LCASE() Function The LCASE() function converts the value of a field to lowercase. SQL LCASE() Syntax SELECT LCASE(column_name) FROM table_name Syntax for SQL Server SELECT LOWER(column_name) FROM table_name
3. MID() - Extract characters from a text field The MID() Function The MID() function is used to extract characters from a text field. SQL MID() Syntax SELECT MID(column_name,start[,length]) FROM table_name Parameter column_name Start Length Description Required. The field to extract characters from Required. Specifies the starting position (starts at 1) Optional. The number of characters to return. If omitted, the MID() function returns the rest of the text
4. LEN() - Returns the length of a text field The LEN() Function The LEN() function returns the length of the value in a text field. SQL LEN() Syntax SELECT LEN(column_name) FROM table_name
5. ROUND() - Rounds a numeric field to the number of decimals specified The ROUND() Function The ROUND() function is used to round a numeric field to the number of decimals specified. SQL ROUND() Syntax SELECT ROUND(column_name,decimals) FROM table_name Parameter column_name decimals Description Required. The field to round. Required. Specifies the number of decimals to be returned.
6. NOW() - Returns the current system date and time The NOW() Function The NOW() function returns the current system date and time. SQL NOW() Syntax SELECT NOW() FROM table_name
7.FORMAT() - Formats how a field is to be displayed The FORMAT() Function The FORMAT() function is used to format how a field is to be displayed. SQL FORMAT() Syntax SELECT FORMAT(column_name,format) FROM table_name Parameter column_name Format Description Required. The field to be formatted. Required. Specifies the format.
HINT: http://www.w3schools.com/sql/sql_func_avg.asp
GENERAL Functions:
NVL(column_name, m) - Returns m if column_name value is NULL SELECT NVL(empno,0) FROM emp -- returns 0 if empno is NULL
NVL2(column_name,c1,c2) - Returns c1 if column_name IS NOT NULL and c2 if column_name IS NULL SELECT NVL2(empno,empno,'NO EID') from dual -returns empno if empno IS NOT NULL else returns 'NO EID'
COALESCE(column1,column2,column3,column4) - Returns columns2 if column1 IS NULL, else returns column3 if column2 is also NULL and so on. All the columns should be of same data type
SELECT COALESCE(ename,first_name,last_name,'NO NAME') from emp -returns first_name IF ename IS NULL, IF first_name IS also NULL, then returns last_name, IF l ast_name IS also NULL, then returns 'NO NAME'
DECODE(column_name,val1,ret_val,val2,ret_val2,ret_ val3) - Returns ret_val1 IF SELECT DECODE(empno,1,'PM',2,'BM','EMP') FROM emp -returns 'PM' if empno = 1 ELSE IF empno = 2 then returns 'BM' ELSE returns 'EMP'
CASE(column_name) - CASE can be used in PLSQL as well as SQL statement. SELECT empno,ename,
(CASE WHEN (empno = 1) THEN 'PM' WHEN (empno = 2) THEN 'BM' ELSE 'EMP' END) Position FROM emp