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.

Slides:



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

Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
Using Single-Row Functions to Customize Output
Ch. 3 Single-Row Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
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,
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
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.
Subqueries.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
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.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
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.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Review SQL Advanced. Capabilities of SQL SELECT Statements Selection Projection Table 1 Table 2 Table 1 Join.
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.
Introduction to Functions – Single Row Functions.
Using Single-Row Functions to Customize Output
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
3 Copyright © 2007, Oracle. All rights reserved. Substitution Variables ra Oly l&On nase lce Int erU.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
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.
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.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Enhanced Guide to Oracle 10g
Ch. 3 Single-Row Functions
Single-Row Functions Schedule: Timing Topic 55 minutes Lecture
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Restricting and Sorting Data
Using Single-Row Functions to Customize Output
(SQL) Single-Row Functions
Single-Row Functions Lecture 9.
Review SQL Advanced.
Restricting and Sorting Data
Lecture 5 SQL FUNCTIONS.
Presentation transcript:

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 individual data items Manipulate output for groups of rows Format dates and numbers for display Convert column datatypes SQL functions may accept arguments and always return a value. Note: Most of the functions described in this lesson are specific to Oracle’s version of SQL.

Two Types of SQL Functions Functions Single-rowfunctions Multiple-rowfunctions There are two distinct 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. There are different types of single-row functions. This lesson covers the following ones: Character - Number - Date - Conversion Single-row functions are used to manipulate data items. They accept one or more arguments and return one value for each row returned by the query. An argument can be one of the following: User-supplied constant Variable value Column name Expression

Features of single-row functions: Act on each row returned in the query Return one result per row May return a data value of a different type than that referenced May expect one or more arguments Can be used in SELECT, WHERE, and ORDER BY clauses; can be nested function_name (column|expression, [arg1, arg2,...])

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

Character functions  ccept 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 datatype (All date functions return a value of date datatype except the MONTHS_BETWEEN function, which returns a number.) Conversion functions  Convert a value from one datatype to another General functions: –NVL function –DECODE function

Character Functions Characterfunctions LOWERUPPERINITCAPCONCATSUBSTRLENGTHINSTRLPAD Case conversion functions Character manipulation functions

Using Case Conversion Functions Display the employee number, name, and department number for employee Blake. SQL> SELECTempno, ename, deptno 2 FROMemp 3 WHEREename = 'blake'; no rows selected SQL> SELECTempno, ename, deptno 2 FROMemp 3 WHEREename = 'blake'; no rows selected EMPNO ENAME DEPTNO BLAKE 30 EMPNO ENAME DEPTNO BLAKE 30 SQL> SELECTempno, ename, deptno 2 FROMemp 3 WHERE LOWER(ename) = 'blake';

Example: Display the values in the columns; ename, job, at the same field with a space btween them displying them in lowercase, under a new column name as Employee Details. More over the values in the column job should be initial capitalized and displayed under the column name Employee Job the values in the column ename should be in uppercase and displayed under the column name as Employee name for all the employees whose name starts with letter A.

CONCAT( ' Good ', ' String ' ) SUBSTR( ' String ',1,3) LENGTH( ' String ' ) INSTR( ' String ', ' r ' ) LPAD(sal,10, ' * ' ) TRIM( ‘ S ’ FROM ‘ SSMITH ’ ) GoodString Str 6 3 ******5000 MITH FunctionResult Character Manipulation Functions Manipulate character strings

Character Manipulation Functions 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 numeric position of a named character LPAD  Pads the character value right-justified Trim: Trims heading or trailing characters (or both) from a character string if trim_character or trim_source is a character literal. You must enclose it in single quotes.

Using the Character Manipulation Functions SQL> SELECT ename, CONCAT (ename, job), LENGTH(ename), 2 INSTR(ename, 'A') 3 FROM emp 4 WHERE SUBSTR(job,1,5) = 'SALES'; ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,'A') MARTIN MARTINSALESMAN 6 2 ALLEN ALLENSALESMAN 5 1 TURNER TURNERSALESMAN 6 0 WARD WARDSALESMAN 4 2

Example Modify the SQL statement on the slide to display the data for those employees whose names end with an N.

Number Functions ROUND: Rounds value to specified decimal ROUND(45.926, 2) TRUNC: Truncates value to specified decimal TRUNC(45.926, 2) MOD: Returns remainder of division MOD(1600, 300) 100 ROUND: Rounds value to specified decimal ROUND(45.926, 2) TRUNC: Truncates value to specified decimal TRUNC(45.926, 2) MOD: Returns remainder of division MOD(1600, 300) 100

Using the ROUND Function SQL> SELECT ROUND(45.923,2), ROUND(45.923,0), 2 ROUND(45.923,-1) 3 FROM DUAL; ROUND(45.923,2) ROUND(45.923,0) ROUND(45.923,-1)

SQL> SELECT TRUNC(45.923,2), TRUNC(45.923), 2 TRUNC(45.923,-1) 3 FROM DUAL; TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1) Using the TRUNC Function

Using the MOD Function Calculate the remainder of the ratio of salary to commission for all employees whose job title is salesman. SQL> SELECTename, sal, comm, MOD(sal, comm) 2 FROMemp 3 WHEREjob = 'SALESMAN'; ENAME SAL COMM MOD(SAL,COMM) MARTIN ALLEN TURNER WARD

Working with Dates Oracle stores dates in an internal numeric format: century, year, month, day, hours, minutes, seconds. The default date format is DD-MON-YY. SYSDATE is a function returning date and time. DUAL is a dummy table used to view SYSDATE. Oracle stores dates in an internal numeric format: century, year, month, day, hours, minutes, seconds. The default date format is DD-MON-YY. SYSDATE is a function returning date and time. DUAL is a dummy table used to view SYSDATE.

Arithmetic with Dates Add or subtract a number to or 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. Add or subtract a number to or 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.

You can perform the following operations:

Using Arithmetic Operators with Dates SQL> SELECT ename, (SYSDATE-hiredate)/7 WEEKS 2 FROM emp 3 WHERE deptno = 10; ENAME WEEKS KING CLARK MILLER

Date Functions Number of months between two dates MONTHS_BETWEEN ADD_MONTHS NEXT_DAY LAST_DAY ROUND TRUNC Add calendar months to date Next day of the date specified Last day of the month Round date Truncate date FunctionDescription

MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')MONTHS_BETWEEN ('01-SEP-95','11-JAN-94') Using Date Functions ADD_MONTHS ('11-JAN-94',6)ADD_MONTHS ('11-JAN-94',6) NEXT_DAY ('01-SEP-95','FRIDAY')NEXT_DAY ('01-SEP-95','FRIDAY') LAST_DAY('01-SEP-95')LAST_DAY('01-SEP-95') '11-JUL-94' '08-SEP-95' '30-SEP-95'

e.g. (using date functions ) Date Functions (continued) For all employees employed for fewer than 200 months, display the employee number, hire date, number of months employed, six- month review date, and last day of the month when hired. SQL> SELECT empno, hiredate, 2MONTHS_BETWEEN(SYSDATE, hiredate) TENURE, 3ADD_MONTHS(hiredate, 6) REVIEW, 4LAST_DAY(hiredate) 5 FROMemp 6 WHEREMONTHS_BETWEEN (SYSDATE, hiredate)<200;

Using Date Functions ROUND('25-JUL-95','MONTH') 01-AUG-95ROUND('25-JUL-95','MONTH') 01-AUG-95 ROUND('25-JUL-95','YEAR') 01-JAN-96ROUND('25-JUL-95','YEAR') 01-JAN-96 TRUNC('25-JUL-95','MONTH') 01-JUL-95TRUNC('25-JUL-95','MONTH') 01-JUL-95 TRUNC('25-JUL-95','YEAR') 01-JAN-95TRUNC('25-JUL-95','YEAR') 01-JAN-95

Example Compare the hire dates for all employees who started in Display the employee number, hire date, and month started using the ROUND and TRUNC functions. SQL> SELECTempno, hiredate, 2ROUND(hiredate, 'MONTH'), TRUNC(hiredate, 'MONTH') 3 FROMemp 4 WHEREhiredate like '%82';