Single Row Functions Part I Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Advertisements

Objectives After completing this lesson, you should be able to do the following: Describe various types of conversion functions that are available in.
Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Using Single-Row Functions to Customize Output
Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Ch. 3 Single-Row Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
3-1 Copyright  Oracle Corporation, All rights reserved. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
3 Single-Row Functions. 3-2 Objectives At the end of this lesson, you should be able to: Describe various types of functions available in SQL Use character,
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
Number Functions. 2 home back first prev next last Review single-row character functions –character case-manipulation functions  LOWER, UPPER, INITCAP.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
2-1 Copyright © Oracle Corporation, All rights reserved. Character Strings and Dates Character strings and date values are enclosed in single quotation.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
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 Lecture 9. SQL Functions Functions are very powerful feature of SQL and can be used to do the following:  Perform a calculation.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
After completing this lesson, you should be able to do the following: Describe various types of functions available in MySQL Use character, number, and.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
SQL Oracle PL/SQL. Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
A Guide to SQL, Seventh Edition. Objectives Understand how to use functions in queries Use the UPPER and LOWER functions with character data Use the ROUND.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
2-1 Copyright © Oracle Corporation, All rights reserved. Using the NULL Conditions Test for nulls with the IS NULL operator. SELECT last_name, manager_id.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Conversion Functions.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
Single-Row Functions. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
Copyright س Oracle Corporation, All rights reserved. 3 Single-Row Functions.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
3 第三讲 Single-Row Functions. Objectives After completing this lesson, you should be able to do the following: Describe various types of functions available.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Using Single-Row Functions to Customize Output
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
3 Copyright © 2007, Oracle. All rights reserved. Substitution Variables ra Oly l&On nase lce Int erU.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Defining a Column Alias
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Open Source Server Side Scripting MySQL Functions
Single-Row Functions Schedule: Timing Topic 55 minutes Lecture
Using Single-Row Functions to Customize Output
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Single-Row Functions Schedule: Timing Topic 55 minutes Lecture
Restricting and Sorting Data
Using Single-Row Functions to Customize Output
Writing Basic SQL SELECT Statements
Using Single-Row Functions to Customize Output
Single-Row Functions Lecture 9.
Writing Basic SQL SELECT Statements
Lecture 5 SQL FUNCTIONS.
Presentation transcript:

Single Row Functions Part I Week 2

Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions

Functions arg 1 arg 2 arg n Function Result All functions input 0, 1 or more arguments and output a single result. 2 basic types in SQL: Single row and Group

Single Row Functions –Act on each individual row selected –Return one result per row retrieved from table –Can be nested –Function can be used as part of expression in: SELECT clause, WHERE clause, ORDER BY clause, … –Argument for function may be any expression (literals, columns, arithmetic operators, …

Conversion Character Number Date General Single-rowfunctions Types of Single Row Functions

Characterfunctions LOWERUPPERINITCAP CONCATSUBSTRLENGTHINSTRLPADTRIM Case conversion functions Character manipulation functions Single Row Character Functions

Case Conversion Functions FunctionResult LOWER( ' Intro to SQL ' ) UPPER( ' Intro to SQL ' ) INITCAP( ' Intro to SQL ' ) intro to sql INTRO TO SQL Intro to Sql – Convert case of character string data – Useful for matching when unsure of case used for column’s data or when case varies by row by row – UPPER: converts all characters to upper case – LOWER: converts all characters to lower case – INITCAP: converts first character of each word to upper case and remaining to lower case

SQL> SELECTemployee_id, last_name, department_id FROMemployees WHERE last_name = ‘king'; no rows selected SQL> SELECTemployee_id, last_name, department_id FROMemployees WHERE last_name = ‘king'; no rows selected EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 SQL> SELECTemployee_id, last_name, department_id FROMemployees WHERE LOWER(last_name) = ‘king'; SQL> SELECTemployee_id, last_name, department_id FROMemployees WHERE last_name = UPPER(‘King‘); no rows selected SQL> SELECTemployee_id, last_name, department_id FROMemployees WHERE last_name = UPPER(‘King‘); no rows selected Case Conversion Examples Display employee number, name and department number for employees named King

EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 SQL> SELECTemployee_id, last_name, department_id FROMemployees WHERE INITCAP(last_name) = ‘King'; Case Conversion Examples (ctd) Display employee number, name and department number for employees named King EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 SQL> SELECTempno, last_name, department_id FROMemployees WHERE UPPER(last_name) = ‘KING';

Case Conversion (ctd) Recommended: always use a case conversion function with column character data (except for unusual circumstances where case is significant in the data) and also always use a case conversion function with character substitution variables (except for unusual circumstances where case is significant in the data) EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 EMPLOYEE_ID LAST_NAME DEPARTMENT_ID 100 King 90 SQL> SELECTemployee_id, last_name, department_id FROMemployees WHERE UPPER(last_name) = UPPER(‘&last_name’);

Character Manipulation Functions CONCAT(string1, string2)  Joins 2 character strings together (You are limited to using only two parameters with CONCAT) Character Manipulation Functions SUBSTR(string, start_position, length)  Extracts a string of determined length from a specified starting position Character Manipulation Functions LENGTH(string)  Shows the length of a string as a numeric value Character Manipulation Functions INSTR(string, character)  Finds numeric position of first occurrence of a specified character Character Manipulation Functions LPAD(string, length, character)  Places occurrences of a character (a blank is the default character) to the left of a string to end up with a specified length of character string Character Manipulation Functions RPAD: pads a character value right-justified on a string Character Manipulation Functions TRIM([{LEADING | TRAILING | BOTH}] character FROM string): removes occurrences of a leading and/or trailing (default is both leading and trailing) character (a blank is the default character) from a string

CONCAT( ' Good ', ' Day ' ) SUBSTR( ' Good ',3,2) LENGTH( ' Good ' ) INSTR( ' Good ', ' o ' ) LPAD(last_name,20, ' * ' ) TRIM(‘$‘ FROM ‘$2,345‘) GoodDay od 4 2 ****************King 2,345 FunctionResult Examples using Character Manipulation Functions

Number Functions – Manipulate numeric values; frequently used functions include: ROUND, TRUNC, MOD ROUND(number, n): rounds number to n decimal places ROUND(43.826, 2)  ROUND(43.826, 0)  44 ROUND(43.826, -1)  40 TRUNC(number, n):truncates value to n decimal places TRUNC(43.826, 2)  TRUNC(43.826, 0)  43 TRUNC(43.826, -1)  40 MOD(number1, number2): returns remainder of number1 divided by number2 MOD(17, 3)  2

Date Functions – manipulate date data: most perform calculations on dates ADD_MONTHS(date, number): add or subtract a number of months from a date ADD_MONTHS(date, number): add or subtract a number of months from a date ADD_MONTHS(’22-JAN-01’,6)  22-JUL-01 MONTHS_BETWEEN(date1, date2) : number of months between dates MONTHS_BETWEEN(date1, date2) : number of months between dates MONTHS_BETWEEN(’22-JAN-01’, ’22-JUL-01)  -6 NEXT_DAY(date, day) returns the date for the next ‘day of the week’ from the date specified NEXT_DAY(date, day) returns the date for the next ‘day of the week’ from the date specified NEXT_DAY(’22-JAN-01’,’FRIDAY’)  26-JAN-01 LAST_DAY(date) returns the last day of the month for the date given LAST_DAY(date) returns the last day of the month for the date given LAST_DAY(’22-JAN-01’)  31-JAN-01

Date Functions (ctd) ROUND function can also be used on dates, rounding a date to the nearest month or year ROUND function can also be used on dates, rounding a date to the nearest month or year ROUND(’22-JAN-01’, ‘MONTH’)  01-FEB-01 ROUND(’22-JAN-01’,’YEAR’)  01-JAN-01 TRUNC function can also be used on dates, truncating a date to the nearest month or year TRUNC function can also be used on dates, truncating a date to the nearest month or year TRUNC(’22-JAN-01’, ‘MONTH’)  01-JAN-01 TRUNC(’22-JAN-01’,’YEAR’)  01-JAN-01

Querying Data Not Found in a Table Sometimes you may want to display data not stored in a table Sometimes you may want to display data not stored in a table However format of SELECT statement requires a FROM clause However format of SELECT statement requires a FROM clause In these situations refer to a small public table called DUAL which consists of one row of data for a single column and which therefore results in a single row of output In these situations refer to a small public table called DUAL which consists of one row of data for a single column and which therefore results in a single row of output Example: display today’s date: Example: display today’s date: SELECT SYSDATE FROM DUAL