Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Return values.
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
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 ◦
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
Strings, if/else, return, user input
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.
Using Single-Row Functions to Customize Output
SQL Use of Functions Character functions Please use speaker notes for additional information!
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.
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.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
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.
Oracle Database Administration Lecture 3  Transactions  SQL Language: Additional information  SQL Language: Analytic Functions.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
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 Curt Hill SQL Queries Yet Another Set of Query Features.
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.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
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.
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.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
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 © 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.
CS 3630 Database Design and Implementation
Open Source Server Side Scripting MySQL Functions
What’s So Great About PL/SQL
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Restricting and Sorting Data
SQL 101 3rd Session.
CS 3630 Database Design and Implementation
Computations Done on table data
Using Single-Row Functions to Customize Output
SQL 101 2nd Session.
Single-Row Functions Lecture 9.
VBScript Session 10.
Inside Module 8 Extracting Data Page Using the Extract command 2
Lecture 5 SQL FUNCTIONS.
Trainer: Bach Ngoc Toan– TEDU Website:
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

Lecture 6 29/1/15

Number functions Number functions take numbers as input, change them, and output the results as numbers. 2

Round() This is used to round values up or down and to specify the number of decimal places. To see this, run Select round( ,2), round( ,3), round( ,1) from sys.dual; This will output ROUND( ,2) ROUND( ,3) ROUND( ,1)

Trunc() Truncating is similar to rounding. We specify therequired number of decimal places but Oracle doesn’t round up or down. It simply “chops off” extra digits. To see the difference, examine the following select round( ,2), trunc( ,2) from sys.dual; Will return ROUND( ,2) TRUNC( ,2)

Sign() This is used to show if a value is zero, positive, or negative. ™ 1 is returned if the number is positive ™ -1 is returned if the number is negative ™ 0 is returned if the number is zero i.e. select sign( ) from sys.dual will return –1. 5

CEIL() Raises the value of the number to the next highest integer. For example, Ceil( ) Returns

Floor() Lowers the value to the next lowest integer. For example Floor(123.89) Returns 123 7

Power() and others POWER Raises the number given to the power given. Power(12,2) Raises 12 to the power of 2. select power(12,2) from sys.dual; Answer: Others There are other numerical functions which Oracle can use. They are straight forward and easy to use. Other functions include SQRT (square root), ABS (absolute value), MOD (modulus), LOG (logarithmic), SIN (sine value), COS (cosine value), TAN (tangent value). There are several more. 8

Dual DUAL is a table owned by the SYS user that contains a single VARCHAR2 column called DUMMY and a single row with the value 'X' in it. This table is handy when you want to select a pseudocolumn such as SYSDATE or simply select an expression and only want to get a single row back. SQL> DESC sys.dual Name Null? Type DUMMY VARCHAR2(1) 9

More character functions

Substr This stands for substring. It returns a part of a string. We specify which part of the string we want to return. For example select substr('Diploma',2,3) from sys.dual; ipl 11

INSTR() INSTR() is used to find where characters occur in a string. For example, Instr(‘Diploma’,’o’) Would return the number 5. select instr('Diploma','oma') from sys.dual; Return????? 12

INSTR() Continued.. We can change the syntax slightly. So far we have searched for the 1st occurrence. We can also search for further occurrences. For example select instr('Seventy','e',3) from sys.dual; 14

Ltrim() Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running ltrim(‘ Oracle’) Will remove the leading spaces. Ltrim(‘spacious’,’s’) Will return pacious (the leading s has been removed) 16

Ltrim() Continued.. select Ltrim('spacious','p') from sys.dual; Will return ???? The order specified for the leading characters is not important. For example, Ltrim(‘spacious’,’ps’) Is the same as Ltrim(‘spacious’,’sp’) select Ltrim('spacious','sp') from sys.dual; select Ltrim('spacious','ps') from sys.dual; RTRIM Is the same as LTRIM, except it trims from the right. select rtrim('spacious','su') from sys.dual; Spacio select rtrim('spacious','soui') from sys.dual; spac 19

Another Example select ltrim(emp_name, 'H') from employee; EARNE BYRNE WALSH ARTE DOHERTY MARTIN; 21

Another example SELECT LTRIM(emp_name, 'H' ) "Employee Name" FROM employee WHERE emp_name LIKE 'H%'; Employee Name EARNE ARTE 22

LPAD 23 Lpad is used to “pad” columns/strings to the left. To see this let us take the following string. Let us say that we want the string to appear as being10 characters in length. If we say that we want it to be padded to the left, it would appear like -

LPAD Continued… If we padded with ‘*’it would look like this The syntax for this would be Lpad(‘diploma’,10,’*’) Lpad the word ‘diploma’ so that it is 10 characters long, with extra spaces to the left being filled with *’s. RPAD Rpad, does the same, except that it pads to the right. What will the following command do ? Rpad (‘course’,12) 24 select Lpad('diploma',10,'*') from sys.dual; ***diploma select rpad('diploma',10,'*') from sys.dual; diploma***

Example LPAD('tech', 7);would return ' tech' LPAD('tech', 2);would return 'te' LPAD('tech', 8, '0'); would return '0000tech' LPAD('tech on the net', 15, 'z'); would return 'tech on the net' LPAD('tech on the net', 16, 'z'); would return 'ztech on the net' SELECT LPAD('Good',10,'.'), RPAD('Good',10,'.') FROM dual; GoodGood

Length() Length() returns the length of a string. For example select length('Oracle') from sys.dual; 27

Translate Translate is used to change characters. select translate('SMITH','I','O') from sys.dual; Will change all letter I’s to letter O’s in the string SMITH. select translate('HEEEEEEEEEEELP','E','A') from sys.dual; We can also specify more than 1 character to translate. select translate('HEEEEEEEEEEELP','LP','AA') from sys.dual; 28

REPLACE() Replace is similar to translate. With translate there must be a match between the number of characters to change and the number of characters to change with. I.e. we can’t replace X with TR. We can only replace 1 character with 1 character, 2 with 2, etc. With replace we can do this. For example SELECT replace(job,'ANALYST','BUSANALYST') AS NEWTITLE from EMPLOYEE; Will search the job column and replace all occurrences of ANALYST with BUSANALYST. 29