SQL functions manipulate values and return a single value. If possible SQL functions will perform implicit datatype conversion. Most functions called with NULL will return NULL.
Function (argument1, argument2, ...)
Functions are scalar or aggregate in nature.
| Scalar | Aggregate |
|---|---|
| Act on a single row | Act on a group of rows |
| Return a single value for each row queried | Return a single value for a group of rows |
| Can appear in a SELECT statement if the SELECT statement does not contain a GROUP BY clause | Can be used in select lists and HAVING clauses |
| Can appear in WHERE, START WITH and CONNECT BY clauses | If used in a SELECT with a GROUP BY clause then the GROUP BY must include all columns not affected by the function |
| Can be nested |
Some functions do not return NULL values.
| CONCAT | String concatenation |
| DECODE | Explicit conversions, translates values into specified codes. |
| DUMP | Dumps a value |
| NVL | NULL value substitution where if a NULL is found it is replaced with the given value |
| REPLACE | String replacement |
DECODE translates a column value pr expression into some other value. Certain values which the column value or expression may have can be specified. For each value list a code into which the value can be translated. If the column value or expression's value is not in the list of values then SQL uses the default code for translation.
DECODE (<column_name/expression>, <value_1>, <code_1>, <value_2>, <code_2>, ..., <value_n>, <code_n>, <default code>);
DECODE is neither a character nor a numeric function because it can return values of any type. DECODE could be used to determine a range in whicha value falls. The application of the DECODE function can present a code for that range. DECODE can effectively serve the same purpose as multiple IF-THEN_ELSE statements or a CASE or SWITCH statement if PL/SQL has a CASE or SWITCH statement.
SELECT NAME "Name" ,SALARY "Salary" ,DECODE ( TRUNC(SALARY/1000,0) ,50,1 ,60,2 ,70,3 ,80,4 ,90,5 ,100,6 ,110,7 ,"Too Expensive" ) "Shares" FROM EMPLOYEES;
select owner, dump(table_name) from all_tables; OWNER ------------------------------ DUMP(TABLE_NAME) ---------------------------------------------------------------------------------- SYS Typ=1 Len=4: 68,85,65,76 SYS Typ=1 Len=20: 83,89,83,84,69,77,95,80,82,73,86,73,76,69,71,69,95,77,65,80 SYS Typ=1 Len=19: 84,65,66,76,69,95,80,82,73,86,73,76,69,71,69,95,77,65,80 SYS Typ=1 Len=21: 83,84,77,84,95,65,85,68,73,84,95,79,80,84,73,79,78,95,77,65,80
Subtraction of one date value from another when both dates are passed through the TO_DATE function gives the number of days between the two dates. Use of dates in queries requires use of the TO_DATE() function in order to convert between external strings and internal Oracle date representation.
Oracle stores dates internally in Oracle7 as a seven-byte field containing two decimal digits per byte.
| year | month | day | hour | minute | second |
|---|---|---|---|---|---|
| YYYY (2 bytes) | MM | DD | hh | mi | ss |
New Year's eve 1999 would be stored in Oracle internally as shown below.
| Bytes | Value |
|---|---|
| 1 | 19 |
| 2 | 99 |
| 3 | 12 |
| 4 | 31 |
| 5 | 23 |
| 6 | 45 |
| 7 | 00 |
ROUND(TO_DATE('28-FEB-99 11:15 PM', 'DD-MON-YY HH:MI PM')) gives 01-MAR-99 12:00 AM.
TRUNCDATE(TO_DATE('29-FEB-00 11:15 PM', 'DD-MON-YY HH:MI PM')) gives 29-FEB-00.
In general implicit datatype conversions are performed between compatible datatypes. Explicit datatype conversions can help performance.
TO_CHAR(TO_DATE('12-DEC-99'), 'DDth "of" MONTH, YYYY') gives 12th of DECEMBER, 1999.
| Format | Description |
|---|---|
| YYYY | Four digit year |
| YY | Two digit year |
| MM | Two digit month |
| MON | Upper case three character month |
| Month | Mixed case month |
| DD | Two digit day |
| DAY | Upper case day |
| DDth | Two digit day with st, nd or rd. |
Group functions act on groups of values and not individual items. Use group functions in conjunction with SELECT, INSERT, UPDATE and DELETE statements.
select owner, vsize(table_name) from all_tables; OWNER VSIZE(TABLE_NAME) ------------------------------ ----------------- SYS 4 SYS 20 SYS 19 SYS 21 SYS 13 SYS 8 SYSTEM 13 SCOTT 4 SCOTT 3 SCOTT 5 SCOTT 8 DEMO 8 DEMO 10 DEMO 3 DEMO 8 DEMO 12 DEMO 7 DEMO 5 DEMO 8 DEMO 11 DEMO 4
Expressions can exist in many forms and can be used in the following situations.