14
Single-Row Functions

Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Embed Size (px)

Citation preview

Page 1: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Single-Row Functions

Page 2: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Two Types of SQL Functions

There are two distinct types of functions:• Single-row functions• Multiple-row functions• Single-Row FunctionsThese functions operate on single rows only and return

one result per row. There are different types of single-row functions

• Multiple-Row FunctionsFunctions can manipulate groups of rows to give one

result per group of rows. These functions are known as group functions

Page 3: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Single-Row Functions

Single row functions:• Manipulate data items• Accept arguments and return one value• Act on each row returned• Return one result per row• May modify the data type• Can be nested• Can be used in SELECT, WHERE, and ORDER BY clauses; can

be nested• Accept arguments which can be a column or an expressionfunction_name [(arg1, arg2,...)]

Page 4: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Single-Row Functions

This lesson covers the following single -row functions:

• Character functions: ccept character input and can return both character and number values

• Number functions: Accept numeric input and return numeric values

Page 5: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Character Functions

LOWERUPPERINITCAP

SUBSTRLENGTH

Page 6: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Case Manipulation Functions

• These functions convert case for character strings.

Function result

LOWER(‘SQL Course’) Sql course

UPPER(‘SQL Course’) SQL COURSE

INITCAP(‘SQL Course’) Sql Course

SELECT 'The job id for '||UPPER(last_name)||' is '||LOWER(job_id) AS "EMPLOYEE DETAILS"FROM employees;

Page 7: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Using Case Manipulation Functions

• Display the employee number, name, and department number for employee Higgins:

• SELECT employee_id, last_name, department_idFROM employeesWHERE last_name = 'higgins';no rows selected• SELECT employee_id, last_name, department_idFROM employeesWHERE LOWER(last_name) = 'higgins';

Page 8: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Character-Manipulation Functions

Function Purpose

LENGTH(Column\expression) Returns the number of characters in the expression

SUBSTR(column|expression,m,[n]) Returns specified characters from character value starting at character position m,n character long (if m is negative the count starts and the end of the character value . If n is omitted all characters to thee end of the string are returned

Page 9: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Character-Manipulation Functions

• These functions manipulate character strings:

Function Result

LENGTH('HelloWorld') 10

SUBSTR('HelloWorld',1,5) Hello

Page 10: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Using the Character-Manipulation Functions

• SELECT employee_id, job_id, LENGTH (last_name) FROM employees WHERE SUBSTR(job_id, 4) = 'REP';

Page 11: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Number Functions

• ROUND: Rounds value to specified decimal• ROUND(column|expression, n) Rounds the column, expression, or value

to n decimal places, or, if n is omitted, no decimal places. • ROUND(45.926, 2) 45.93• TRUNC: Truncates value to specified decimal• TRUNC(column|expression,n) Truncates the column, expression, or value

to n decimal places, or, if n is omitted, then n defaults to zero • TRUNC(45.926, 2) 45.92• MOD: Returns remainder of division• MOD(m,n) Returns the remainder of m divided by n • MOD(1600, 300) 100

Page 12: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Using the ROUND Function

• SELECT ROUND(45.923,2), ROUND(45.923,0)FROM DUAL;

• DUAL is a dummy table you can use to view results from functions and calculations.

Page 13: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Using the TRUNC Function

• SELECT TRUNC(45.923,2), TRUNC(45.923)FROM DUAL;

Page 14: Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions

Using the MOD Function

• Calculate the remainder of a salary after it is divided by 5000 for all employees whose job title is sales representative.

• SELECT last_name, salary, MOD(salary, 5000)FROM employeesWHERE job_id = 'SA_REP';