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

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

Here are 3 practice queries using functions: 1. Display the current date: SELECT SYSDATE(); 2. Calculate years of service for employee with ID 100 and hire date 1/1/2015: SELECT FLOOR(MONTHS_BETWEEN(SYSDATE(),TO_DATE('1/1/2015','MM/DD/YYYY'))/12) YEARS_SERVICE FROM DUAL; 3. Display uppercased last name for employees in department 90: SELECT UPPER(last_name) FROM employees WHERE department_id=90;

Uploaded by

Iulian Neaga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

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

Here are 3 practice queries using functions: 1. Display the current date: SELECT SYSDATE(); 2. Calculate years of service for employee with ID 100 and hire date 1/1/2015: SELECT FLOOR(MONTHS_BETWEEN(SYSDATE(),TO_DATE('1/1/2015','MM/DD/YYYY'))/12) YEARS_SERVICE FROM DUAL; 3. Display uppercased last name for employees in department 90: SELECT UPPER(last_name) FROM employees WHERE department_id=90;

Uploaded by

Iulian Neaga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Usage of Single-Row Functions to Customize Output

Agenda

• Single-row SQL functions


• Character functions
• Nesting functions
• Number functions
• Working with dates
• Date 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
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:
• 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
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)
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
• CONCAT(column1|expression1,column2|expression2) -
concatenation function
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
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

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


'|| LOWER(job_id) AS "EMPLOYEE DETAILS"
FROM employees;
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';
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

• 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
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';
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;
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)

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


Using the Numeric Functions

SELECT ROUND(45.923,2),ROUND(45.923,0),ROUND(45.923,-1)

SELECT TRUNCATE(45.923,2), TRUNCATE(45.923,1),


TRUNCATE(45.923,0)

SELECT last_name, salary, MOD(salary, 5000)


FROM employees
WHERE job_id = 'SA_REP';
Working with Dates
• The MySQL 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';
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();
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
(DATEDIFF, TIMESTAMPDIFF )
• Add hours to a date
SELECT last_name, ??? 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

SELECT date_format( ADDDATE( "2017-06-15", INTERVAL 5


MINUTE ) , '%h-%i' )
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 DATE_ADD(CURDATE(), INTERVAL (13 -


IF(DAYOFWEEK(CURDATE())=1, 8, DAYOFWEEK(CURDATE())))
DAY) AS NEXTFRIDAY

• Display the employee number, hire date, and starting month for all
employees who started in 2004.
SELECT employee_id, hire_date,DATE_FORMAT(hire_date,
'%Y-%m-01')
FROM employees
WHERE hire_date LIKE '%04’;
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
Practice 2
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy