0% found this document useful (0 votes)
4 views

Unit 4 - Usage of Single-Row Functions to Customize Output

The document outlines the usage of single-row SQL functions, including character, number, and date functions, and their applications in customizing output. It explains the characteristics of single-row functions, such as accepting arguments and returning one result per row, and provides examples of various functions like CONCAT, ROUND, and SYSDATE. Additionally, it covers nesting functions and date manipulation functions, emphasizing their importance in SQL operations.

Uploaded by

alin.dobre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 4 - Usage of Single-Row Functions to Customize Output

The document outlines the usage of single-row SQL functions, including character, number, and date functions, and their applications in customizing output. It explains the characteristics of single-row functions, such as accepting arguments and returning one result per row, and provides examples of various functions like CONCAT, ROUND, and SYSDATE. Additionally, it covers nesting functions and date manipulation functions, emphasizing their importance in SQL operations.

Uploaded by

alin.dobre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Course code: BCT048

4 Usage of Single-Row Functions to


Customize Output
Agenda

• Single-row SQL functions


• Character functions
• Nesting functions
• Number functions
• Working with dates
• Date functions

Single Row Functions


Agenda

• Single-row SQL functions


• Character functions
• Nesting functions
• Number functions
• Working with dates
• Date functions

Single Row Functions


SQL Functions

• Functions are a very powerful feature of SQL. They can be used to


do the following:

• Perform calculations on data


• Modify individual data items
• Manipulate output for groups of rows
• Format dates and numbers for display
• Convert column data types

• SQL functions sometimes take arguments and always return a


value

Single Row Functions


SQL Functions
• There are two types of functions:
• Single-row functions
• Multiple-row functions
Single-Row Functions
These functions operate on single rows only and return one result per
row. This lesson covers the following functions:
• Character
• Number
• Date
• Conversion
• General
Multiple-Row Functions
Functions can manipulate groups of rows to give one result per group
of rows. These functions are also known as group functions

Single Row Functions


Single-Row Functions
Single-row functions:
• Manipulate data items
• Accept arguments and return one value
• Act on each row that is returned
• Return one result per row
• May modify the data type
• Can be nested
• Accept arguments that can be a column or an expression

Syntax:
function_name [(arg1, arg2,...)]

Single Row Functions


Single-Row Functions
This lesson covers the following single-row functions:

• Character functions: Accept character input and can return both


character and number values

• Number functions: Accept numeric input and return numeric


values

• Date functions: Operate on values of the DATE data type (All


date functions return a value of the DATE data type except the
MONTHS_BETWEEN function, which returns a number)

Single Row Functions


Agenda

• Single-row SQL functions


• Character functions
• Nesting functions
• Number functions
• Working with dates
• Date functions

Single Row Functions


Character Functions
Character functions can be divided into the following:
• Case-conversion functions
• Character-manipulation functions

• LOWER(column|expression) - Converts alpha character values to


lowercase
• UPPER(column|expression) - Converts alpha character values to
uppercase
• INITCAP(column|expression) - Converts alpha character values to
uppercase for the first letter of each word; all other letters in
lowercase
• CONCAT(column1|expression1,column2|expression2) - Equivalent
to concatenation operator (||)

Single Row Functions


Character Functions
• SUBSTR(column|expression,m[,n]) - Returns specified characters
from character value starting at character position m, n characters
long (If m is negative, the count starts from the end of the character
value. If n is omitted return all characters to the end of the string)
• LENGTH(column|expression) - Returns the number of characters
in the expression
• LPAD(column|expression, n, 'string') / RPAD(column|expression,
n, 'string') - Returns an expression left-padded / right-padded to
length of n characters with a character expression
• TRIM(leading|trailing|both,trim_character FROM trim_source) -
Enables you to trim leading or trailing characters (or both) from a
character string
• REPLACE(text,search_string, replacement_string) - Searches a
text expression for a character string and, if found, replaces it with
a specified replacement string

Single Row Functions


Case-Conversion Functions
LOWER, UPPER, and INITCAP are the three case-conversion
functions.
• LOWER: Converts mixed-case or uppercase character strings to
lowercase
• UPPER: Converts mixed-case or lowercase character strings to
uppercase
• INITCAP: Converts the first letter of each word to uppercase and
the remaining letters to lowercase

SELECT 'The job id for '||UPPER(last_name)||' is


'|| LOWER(job_id) AS "EMPLOYEE DETAILS"
FROM employees;

Single Row Functions


Using Case-Conversion Functions
Display the employee number, name, and department number for
employee Higgins:

SELECT employee_id, last_name, department_id


FROM employees
WHERE last_name = 'higgins';

or

SELECT employee_id, last_name, department_id


FROM employees
WHERE LOWER(last_name) = 'higgins';

Single Row Functions


Character-Manipulation Functions
CONCAT, SUBSTR, LENGTH, INSTR, LPAD, RPAD, and TRIM are
the character-manipulation functions that are covered in this lesson:

• CONCAT: Joins values together (You are limited to using two


parameters with CONCAT)

• SUBSTR: Extracts a string of determined length

• LENGTH: Shows the length of a string as a numeric value

• INSTR: Finds the numeric position of a named character

• LPAD / RPAD : Returns an expression left-padded/right-padded to


the length of n characters with a character expression

• TRIM: Trims leading or trailing characters (or both) from a


character string
Single Row Functions
Using the Character-Manipulation Functions
• Displays employee first names and last names joined together, the
length of the employee last name, and the numeric position of the letter
“a” in the employee last name for all employees who have the string,
REP, contained in the job ID starting at the fourth position of the job ID:

SELECT employee_id, CONCAT(first_name, last_name) NAME,


job_id, LENGTH (last_name), INSTR(last_name, 'a')
FROM employees
WHERE SUBSTR(job_id, 4) = 'REP';

• What the next query return?


SELECT employee_id, CONCAT(first_name, last_name) NAME,
job_id, LENGTH (last_name), INSTR(last_name, 'a') FROM
employees
WHERE SUBSTR(last_name, -1, 1) = 'n';

Single Row Functions


Agenda

• Single-row SQL functions


• Character functions
• Nesting functions
• Number functions
• Working with dates
• Date functions

Single Row Functions


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)

SELECT last_name,
UPPER(CONCAT(SUBSTR(LAST_NAME,1,8),'_RO')) Info
FROM employees
WHERE department_id = 60;

Single Row Functions


Agenda

• Single-row SQL functions


• Character functions
• Nesting functions
• Number functions
• Working with dates
• Date functions

Single Row Functions


Numeric Functions
Numeric functions accept numeric input and return numeric values.
This section describes some of the numeric functions

• ROUND(column|expression, n) - Rounds the column, expression,


or value to n decimal places or, if n is omitted, no decimal places (if
n is negative, numbers to the left of decimal point are rounded)

• TRUNC(column|expression, n) - Truncates the column,


expression, or value to n decimal places or, if n is omitted, n
defaults to zero

• MOD(m,n) - Returns the remainder of m divided by n

Single Row Functions


Using the Numeric Functions

SELECT ROUND(45.923,2),ROUND(45.923,0),ROUND(45.923,-1)
FROM DUAL;

• DUAL is a public table that you can use to view results from functions
and calculations

SELECT TRUNC(45.923,2), TRUNC(45.923), TRUNC(45.923,-1)


FROM DUAL;

SELECT last_name, salary, MOD(salary, 5000)


FROM employees
WHERE job_id = 'SA_REP';

Single Row Functions


Agenda

• Single-row SQL functions


• Character functions
• Nesting functions
• Number functions
• Working with dates
• Date functions

Single Row Functions


Working with Dates
• The Oracle Database stores dates in an internal numeric format:
century, year, month, day, hours, minutes, and seconds

• The default date display format is DD-MON-RR


• Enables you to store 21st-century dates in the 20th century by
specifying only the last two digits of the year
• Enables you to store 20th-century dates in the 21st century in the same
way
• When a date is inserted into a table, the century information is picked
up from the SYSDATE (century component is not displayed - by default)

SELECT last_name, hire_date


FROM employees
WHERE hire_date < '01-FEB-08';

Single Row Functions


Working with Dates

The RR date format is similar to the YY element, but you can use it to
specify different centuries. Use the RR date format element instead of YY
so that the century of the return value varies according to the specified
two-digit year and the last two digits of the current year. The table in the
slide summarizes the behavior of the RR element.

Single Row Functions


The SYSDATE Function

SYSDATE is a function that returns:

• Date
• Time

• SYSDATE is a date function that returns the current database server


date and time

SELECT sysdate
FROM dual;

Single Row Functions


Arithmetic with Dates

• Add to or subtract a number from a date for a resultant date value


• Subtract two dates to find the number of days between those dates
• Add hours to a date by dividing the number of hours by 24

SELECT last_name, (SYSDATE-hire_date)/7 AS WEEKS


FROM employees
WHERE department_id = 90;

• If a more current date is subtracted from an older date, the difference is


a negative number

Single Row Functions


Agenda

• Single-row SQL functions


• Character functions
• Nesting functions
• Number functions
• Working with dates
• Date functions

Single Row Functions


Date-Manipulation Functions

• MONTHS_BETWEEN - Number of months between two dates

• ADD_MONTHS - Add calendar months to date

• NEXT_DAY - Week day of NEXT_DAY the date specified

• LAST_DAY - Last day of the month

• ROUND - Round date

• TRUNC - Truncate date

Single Row Functions


Using Date-Manipulation Functions
• Display the employee number, hire date, number of months employed,
six-month review date, first Friday after hire date, and the last day of the
hire month for all employees who have been employed for fewer than
150 months.
SELECT employee_id, hire_date, MONTHS_BETWEEN (SYSDATE,
hire_date) TENURE, ADD_MONTHS (hire_date, 6) REVIEW,
NEXT_DAY (hire_date,'FRIDAY'), LAST_DAY(hire_date)
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) < 150;
• Display the employee number, hire date, and starting month using the
ROUND and TRUNC functions for all employees who started in 2004.
SELECT employee_id, hire_date,
ROUND(hire_date, 'MONTH'), TRUNC(hire_date, 'MONTH')
FROM employees
WHERE hire_date LIKE '%04‘;
Single Row Functions
Quiz

Which four of the following statements are true about single-row functions?
a. Manipulate data items
b. Accept arguments and return one value per argument
c. Act on each row that is returned
d. Return one result per set of rows
e. May not modify the data type
f. Can be nested
g. Accept arguments that can be a column or an expression

Single Row Functions


Practice 4
This practice covers the following topics:

• Writing a query that displays the current date

• Creating queries that require the use of numeric, character, and date
functions

• Performing calculations of years and months of service for an employee

Single Row Functions

You might also like