SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.

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.
Maths & Trig, Statistical functions. ABS Returns the absolute value of a number The absolute value of a number is the number without its sign Syntax ◦
Functions and string manipulation Lab 2 Week 2. Date functions Commonly used date functions are: –sysdate –next_day –add_months –last_day –months_between.
BIL101, Introduction to Computers and Information Systems Chapter 11 Sample SQL Applications Prepared by Metin Demiralp Istanbul Technical University,
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.
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.
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.
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.
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.
SINGLE ROW FUNCTIONS 1. CHARACTER MANIPULATION Prof. Carmen Popescu Oracle Academy Lead Adjunct.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
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.
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.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
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.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
Using Single-Row Functions to Customize Output
Oracle & SQL. Oracle Data Types Character Data Types: Char(2) Varchar (20) Clob: large character string as long as 4GB Bolb and bfile: large amount of.
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.
1 Inside Module 8 Extracting Data Page n Using the Extract command2 n Coercion3 n $-functions4 n Extract from a table7.
[ ] Square brackets enclose syntax options { } Braces enclose items of which only one is required | A vertical bar denotes options … Three dots indicate.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
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.
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.
Open Source Server Side Scripting MySQL Functions
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
MARK CARPENTER, Ph.D. Introduction to SAS Programming and Applications
Chapter Nine Data Manipulation Language (DML) Functions
SQL 101 3rd Session.
Computations Done on table data
Using Single-Row Functions to Customize Output
Oracle SQL Built-in Functions
SQL 101 2nd Session.
Single-Row Functions Lecture 9.
VBScript Session 10.
Lecture 5 SQL FUNCTIONS.
Trainer: Bach Ngoc Toan– TEDU Website:
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

SQL Functions

SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full for performing processing on string, numeric or date type data.

Two Types of SQL Functions Functions Single-rowFunctions (Scalar Function) Multiple-rowFunctions (Aggregate Or Group function)

Single-Row/Scalar Functions: Act on each row returned Return one result per row Multiple Row/Group/Aggregate Functions Act on set of rows Return one result for set of rows SQL Functions

Single-Row Functions Conversion Character Number Date Single-rowfunctions

Character/String Functions LOWERLPAD UPPERRPAD INITCAPVSIZE SUBSTR INSTR ASCII TRANSLATE CONCAT LENGTH LTRIM RTRIM TRIM

LOWER: - Returns the argument in lowercase. syntax: LOWER(char||field name) example: select LOWER(‘MYSQL') from dual; -> mysql UPPER: -Convert argument to uppercase. syntax: UPPER(char||field name) example: SELECT UPPER('Hej') from dual; -> HEJ Character/String Functions

INITCAP: -Returns string with the first letter of each word in upper case. Syntax: INITCAP(char||field_name) example: select INITCAP(‘my sql') from dual; -> My Sql SUBSTR: –Returns a sub part of string with respect to position and number of string. it takes string, position and no_of_char as argument. Syntax: SUBSTR(string||field, position, no_of_char) Example: select substr(name,3,2) from demo; Character/String Functions

INSTR: - Returns the position of the first occurrence of substring substr in string str. syntax: INSTR(str1,st2,[start_posi]) example: SELECT INSTR('foobarbar', 'bar'); ->4 ASCII: -Return ascii numeric value of character syntax: ASCII(char) example: SELECT ASCII(‘d'); -> 100 Character/String Functions SELECT ASCII('dx'); -> 100

TRANSLATE: - TRANSLATE returns string with all occurrences of each character in to_replace_str replaced by its corresponding replacement_str. syntax: TRANSLATE(string to_replace_str, replacement_str) example: SELECT TRANSLATE(5myNumber123', ‘512‘,’789’); ->7myNumber893 CONCAT: -Returns the string that results from concatenating the arguments. syntax: CONCAT(str1,str2) example: SELECT CONCAT(‘My’,’Sql’); -> MySql Character/String Functions SELECT ASCII('dx'); -> 100

LENGTH: - Returns the length of a string. syntax: LENGTH(‘str’) example: SELECT LENGTH('text') from dual; ->4 LTRIM: - Returns the string str with leading space characters or specified character removed. syntax: LTRIM(str,[char]) example: SELECT LTRIM(' MySQL');-> MySQL SELECT LTRIM(‘ANISHA’,’A’) from dual; ->NISHA Character/String Functions SELECT ASCII('dx'); -> 100

RTRIM: - Returns the string str with trailing space characters or specified character removed from right side. syntax: RTRIM(str,[char]) example: SELECT RTRIM('MySQL ');-> MySQL SELECT LTRIM(‘ANISHA’,’A’) from dual; ->ANISH TRIM: - removes the leading, trailing, both space characters or specified character from string. syntax: TRIM(leading||trailing||both ‘char’ from ‘str’) example: SELECT TRIM(' MySQL ');-> MySQL SELECT TRIM(both ‘A’ from ‘ANISHA’) from dual; ->NISH Character/String Functions SELECT ASCII('dx'); -> 100

LPAD: - Return the string argument, left-padded with the specified character. syntax: LPAD(‘str1’,length,’char’) example: SELECT LPAD('hi',4,'??') from dual; -> '??hi' RPAD: - Return the string argument, right-padded with the specified character. syntax: RPAD(‘str1’,length,’char’) example: SELECT RPAD('hi',5,'?') from dual; -> 'hi???‘ SELECT RPAD('hi',1,'?') from dual; -> 'h' Character/String Functions SELECT ASCII('dx'); -> 100

Numeric Functions ABS POWER SQRT ROUND FLOOR CEIL TRUNC EXP(EXPONENT) GREATEST LEAST MOD

ABS: - Return the absolute value of n. syntax: ABS(n) example: SELECT ABS(-15) from dual; -> 15 POWER: - Returns m raised to n power. ‘n’ must be an integer. syntax: POWER(m,n) example: SELECT POWER(3,2) from dual; -> 9 SELECT POWER(3,3) from dual; -> 27 Numeric Functions

SQRT: - Returns square root of ‘n’. syntax: SQRT(n) example: SELECT SQRT(9) from dual; -> 3 ROUND: - Returns n, rounded to ‘m’ place to the right of decimal point. If ‘m’ is omitted, ‘n’ is rounded to 0 places (without float value). syntax: ROUND(m,[n]) example: SELECT ROUND(14.55) from dual; -> 15 SELECT ROUND(14.555,2) from dual;-> Numeric Functions

FLOOR: - Returns the largest integer value that is less than or equal to a number. syntax: FLOOR(n) example: SELECT FLOOR(24.8) “floor” from dual; -> 24 CEIL: - Returns the integer value that is greater that or equal to a number. syntax: CEIL(n) example: SELECT CEIL(24.8) from dual; -> 25 Numeric Functions

TRUNC: - Returns a number truncated to a certain number of decimal places. If decimal places is omitted,the function will truncate the number to 0 decimal places. syntax: TRUNC(number,[decimal places]) example: select trunc( ,2) from dual; ->15.85 EXP: -Returns e raised to nth power, where e= syntax: EXP(n) example: select EXP(3) from dual; -> Numeric Functions

GREATEST: - Returns greatest value from list of expressions. syntax: GREATEST(expr1,expr2,..expr3) example: select greatest(5,7,8) from dual; -> 8 LEAST: -Returns the least value from list of expression. syntax: LEAST(expr1,expr2,..expr3) example: select least(5,7,8) from dual; -> 5 Numeric Functions

MOD: - Returns remainder of 1 st number divided by second number passed as parameter. syntax: MOD(m,n) example: select mod(4,2) from dual; ->0 select mod(5.5,2) from dual; ->1.5 Numeric Functions

Date Functions To manipulate and extract values from date column oracle provides some date functions: SYSDATE is used to get system date. ADD_MONTHS LAST_DAY MONTHS_BETWEEN NEXT_DAY ROUND EXTRACT

ADD_MONTHS: - Returns the date after adding the number of months specified in function. syntax: ADD_MONTHS(date,no_of_month) example: select add_months(‘21-jan-2014’,4) from dual; -> 21-MAY-14 MONTHS_BETWEEN : -Returns the number of months between date1 and date2. syntax: MONTHS_BETWEEN(date1,date2) example: select months_between('15-jan-2014','15-nov-2013') from dual;- >2 Date Functions

LAST_DAY: - Returns the last date of months specified in function. syntax: LAST_DAY(date) example: select last_day(sysdate) from dual; -> 31-JAN-14 NEXT_DAY: -NEXT_DAY returns the date of the first weekday named by char that is after date named by date. The return type is always DATE, regardless of the datatype of date. The argument char must be a day of the week syntax: NEXT_DAY(date,char) example: select next_day(sysdate,'sunday') from dual;- >26-jab-14 Date Functions

ROUND: - ROUND function returns a date rounded to a specific unit of measure. format parameter: Date Functions UnitValid format parametersRounding Rule YearSYYYY, YYYY, YEAR, SYEAR, YYY, YY, YRounds up on July 1st ISO Year IYYY, IY, I QuarterQRounds up on the 16th day of the second month of the quarter MonthMONTH, MON, MM, RMRounds up on the 16th day of the month Week WW Same day of the week as the first day of the year WWSame day of the week as the first day of the month DayDDD,DD MinuteMI HourHH, HH12,HH24

syntax: ROUND( date, [ format ] ) example: ROUND(TO_DATE ('22-AUG-03'),'YEAR') would return '01-JAN-04' ROUND(TO_DATE ('22-AUG-03'),'Q') would return '01-OCT-03' ROUND(TO_DATE ('22-AUG-03'),'MONTH') would return '01-SEP-03' ROUND(TO_DATE ('22-AUG-03'),'DDD') would return '22-AUG-03‘ EXTRACT: EXTRACT function extracts a value from a date or interval value. Syntax: EXTRACT ( { YEAR | MONTH | DAY | HOUR | MINUTE | SECOND } | { TIMEZONE_HOUR | TIMEZONE_MINUTE } | { TIMEZONE_REGION | TIMEZONE_ABBR } FROM { date_value | interval_value } ) Date Functions

Examples: SELECT EXTRACT(YEAR FROM DATE ' ') from dual ->2003 SELECT EXTRACT(MONTH FROM DATE ' ') from dual-> 8 SELECT EXTRACT(DAY FROM DATE ' ') from dual-> 22 SELECT EXTRACT(HOUR FROM TO_TIMESTAMP('01-JAN :15:26', 'DD-MON-YYYY HH24:MI:SS')) AS HOUR FROM dual; ->19 Date Functions