SQL Single Row Functions PDF
SQL Single Row Functions PDF
Input Output
Function
arg n
SQL Functions Types
SQL Functions
Single-row Multiple-row
functions functions
Character
Single-row
General Number
functions
Conversion Date
Character Functions
Character
functions
Case-manipulation Character-manipulation
functions functions
LOWER CONCAT
UPPER SUBSTR
INITCAP LENGTH
INSTR
LPAD | RPAD
TRIM
REPLACE
Case Manipulation Functions
LOWER: Converts mixed case or uppercase character
string to lowercase.
Function Result
1 2 3
Number Functions
Function Result
ROUND(45.926, 2) 45.93
TRUNC(45.926, 2) 45.92
MOD(1600, 300) 100
ROUND Function
1 2
SELECT ROUND(45.923,2), ROUND(45.923,0),
ROUND(45.923,-1) 3
FROM DUAL;
1 2 3
DUAL is a dummy table that you can use to view results from
functions and calculations.
TRUNC Function
1 2
SELECT ROUND(45.923,2), ROUND(45.923),
ROUND(45.923,-1) 3
FROM DUAL;
1 2 3
MOD Function
For all employees with job title of Sales Representative,
calculate the remainder of the salary after it is divided by 5,000.
Function Result
MONTHS_BETWEEN Number of months between two dates
ADD_MONTHS Add calendar months to date
NEXT_DAY Next day of the date specified
LAST_DAY Last day of the month
ROUND Round date
TRUNC Truncate date
Using Date Functions
Function Result
Function Result
ROUND(SYSDATE,'MONTH') 01-AUG-03
ROUND(SYSDATE ,'YEAR') 01-JAN-04
TRUNC(SYSDATE ,'MONTH') 01-JUL-03
TRUNC(SYSDATE ,'YEAR') 01-JAN-03
Conversion Functions
Data type
conversion
From To
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
NUMBER VARCHAR2
DATE VARCHAR2
Implicit Data Type Conversion
For expression evaluation, the Oracle Server can
automatically convert the following:
From To
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
Notes:
CHAR to NUMBER conversions succeed only if the
character string represents a valid number.
CHAR to DATE conversions succeed only if the character
string has the default format DD-MON-YY.
Explicit Data Type Conversion
TO_NUMBER TO_DATE
TO_CHAR TO_CHAR
TO_CHAR Function with Dates
TO_CHAR(date,'format_model')
These are some of the format elements that you can use
with the TO_CHAR function to display a number value as a
character:
Element Result
9 Represents a number
0 Forces a zero to be displayed
$ Places a floating dollar sign
L Uses the floating local currency symbol
. Prints a decimal point
, Prints a comma as thousands indicator
TO_CHAR Function with Numbers
SELECT TO_CHAR(salary, '$99,999.99') SALARY
FROM employees
WHERE last_name = 'Ernst';
NOTES
• The Oracle Server rounds the stored decimal value to the
number of decimal spaces provided in the format model.
TO_NUMBER(char[,'format_model'])
1 2
NVL2 Function
SELECT last_name, salary, commission_pct, 1
NVL2(commission_pct,
'SAL+COMM', 'SAL') income 2
FROM employees WHERE department_id IN (50, 80);
1 2
NULLIF Function
1
SELECT first_name, LENGTH(first_name) "expr1",
last_name, LENGTH(last_name) "expr2", 2
NULLIF(LENGTH(first_name), LENGTH(last_name)) result 3
FROM employees;
1 2 3
Nesting Functions
• Single-row functions can be nested to any level.
• Nested functions are evaluated from the deepest level to
the least deep level.
F3(F2(F1(col,arg1),arg2),arg3)
Step 1 = Result 1
Step 2 = Result 2
Step 3 = Result 3
Nesting Functions
SELECT last_name,
UPPER(CONCAT(SUBSTR (LAST_NAME, 1, 8), '_US'))
FROM employees
WHERE department_id = 60;
THANK YOU