Computations Done on table data

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.
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 ◦
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
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.
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
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
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,
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 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 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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
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.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
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.
Single Row Functions. 3-2 Objectives Explain the various types of functions available in SQL. Explain the various types of functions available in SQL.
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.
Lesson 3: Using Formulas
Topics Designing a Program Input, Processing, and Output
Open Source Server Side Scripting MySQL Functions
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Writing Basic SQL SELECT Statements
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic select statement
The Selection Structure
Unit 42 : Spreadsheet Modelling
Writing Basic SQL SELECT Statements
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Using Single-Row Functions to Customize Output
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Unit-3 Interactive SQL.
Single-Row Functions Lecture 9.
Writing Basic SQL SELECT Statements
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Reporting Aggregated Data Using the Group Functions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Inside Module 8 Extracting Data Page Using the Extract command 2
Reporting Aggregated Data Using the Group Functions
Lecture 5 SQL FUNCTIONS.
Reporting Aggregated Data Using the Group Functions
Lab 3: Single-row Functions
REACH Computer Resource Center
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

Computations Done on table data None of the techniques used till now allows display of data from a after some arithmetic has been done it. Computations may include displaying employee salary from the Employee_Master table along with annual salary.

Arithmetic Operators + : Addition - : Subtraction / : Division * : Multiplication ** : Exponentiation () : Enclosed Operation

Logical Operators The AND Operator : Select * from persons where name=‘’ and address=‘’; The OR Operator : Select * from persons where name=‘’ or address=‘’;

Combining AND and OR Operator EX : Select * from persons where (name=‘’ and address=‘’) OR (name=‘’ and address=‘’);

The NOT operator Ex: Select * from persons where NOT ( name=‘a’ OR name=‘b’); The oracle engine will not display rows from the persons table where the value of the field NAME is either a or b.

Range Searching In order to select data that is within a range of values the BETWEEN operator is use. The lower value must be coded first. The values in between the range must be linked with the keyword AND. Data types can not be mixed.

Example Select * from persons where TO_CHAR(DOB,’MM’) BETWEEN 01 AND 03; Display persons birth date between month 1 and 4 Select * from persons where To_CHAR(DOB,’MM) NOT BETWEEN 01 AND 04; Not Display persons Birth Date between 01 and 04

Pattern Matchning The use of LIKE Predicate The LIKE predicate allows comparison of one string value to another string value. For Character Data Types : % : Allow to match any string of any length _ : Allow to match on a single character.

Example Select name from persons where name LIKE ‘a%’; Output : Name Abc Aaa Select name from persons where name LIKE ‘_a%’; Bac aaa

Alias (another name of attribute) Rename the default output column names with an alias, when requried Syntax SELECT <COLUMNNAME><ALIASNAME>, <COLUMNNAME><ALIASNAME> FROM <TABLENAME>; Example SELECT NAME “FIRSTNAME” FROM PERSONS; Output FIRSTNAME abc

The IN and NOT IN predicates The arithmetic operator(=) compares a single value to another single value. Compared to a list of values then the IN predicate is used. The IN predicate helps reduce the need to use multiple OR conditions. Example: List the customer details of the customers named CHINTAN, SANJAY AND VISHAL

SOLUTION SELECT FNAME,LNAME, DOB “BIRTHDATE” FROM PERSONS WHERE FNAME IN (‘CHINTAN’,’SANJAY’,’VISHAL’); FNAME LANME BIRTHDATE CHINTAN SHAH 29-APR-90 SANJAY PATEL 12-JUN-92 VISHAL 22-OCT-90

NOT IN The NOT IN predicate is the opposite of the IN predicate. This will select all the rows where values do not match the values in the list. Example:- List the customer details of the customer other than CHINTAN,SANJAY AND VISHAL

SOLUTION SELECT FNAME,LNAME,DOB “BIRTHDATE” FROM PERSONS WHERE FNAME NOT IN(‘CHINTAN’,’SANJAY’,’VISHAL’); OUTPUT FNAME LANME BIRTHDATE NITIN PANDYA 29-APR-90 HETAL SHAH 12-JUN-92 KHUSHBU GANDHI 22-OCT-90 MUKESH GANGDIA 18-NOV-70 SAURIN PARIKH 22-FEB-80

ORACLE FUNCTION Oracle functions serve the purpose of manipulating data items and returning a result. Function are also capable of accepting user- supplied variables or constants and operation on them. Such variables or constants are called an arguments. Any number of arguments (or no arguments) can be passed to a function.

Assume table structure with data DUAL table is already in built table in ORACLE. ACCOUNT TABLE ID NAME ACCNO BALANCE DESCRIPTION 1 CHINTAN 0001 25000 SAVINGS 2 SANJAY 0002 20000 CURRENT 3 NITIN 0003 18300 FD 4 KHUSHBU 0004 30000 SALARY 5 HETAL 0005 24000 DEMAT

Aggregate function AVG(AVERAGE): - Returns an average value of ‘n’. Ignoring null value in a column. Syntax: - AVG(COLUMNNAME) Example: - SELECT AVG(BALANCE) “AVERAGE BALANCE” FROM ACCOUNT;

OUTPUT AVERAGE BALANCE 23460

Min and max MIN: - Returns a minimum value of expr. Syntax: - MIN(COLUMNNAME) EXAMPLE: - SELECT MIN(BALANCE) “MINIMUM BALANCE” FROM ACCOUNT;

MIN AND MAX OUTPUT MINUMUM BALANCE 18300 MAX:- Returns the maximum value of expr. Syntax: - MAX(COUMNNAME) Example SELECT MAX(BALANCE) “MAXIMUM BALANCE” FROM PERSONS; MAXIMUM BALANCE 30000

COUNT COUNT(expr): Returns the number of rows where expr is not null. Syntax: - COUNT(COLUMNAME) Example: - SELECT COUNT(ACCNO) “NO OF ACCOUNT” FROM ACCOUNT; Output: NO OF ACCOUNT 5

SUM SUM: - Return the sum of values. Syntax: - SUM(COLUMNNAME) Example: - SELECT SUM(BALANCE) “TOTAL BALANCE” FROM ACCOUNT; Output: - TOTAL BALANCE 117300

Numeric function ABS(ABSOLUTE): - Returns the absolute value of ‘n’. Syntax: - ABS(N) EXAMPLE

power POWER: - Returns m raised to the nth power. n must be an integer, else an error is returned. Syntax: POWER(M,N) EXAMPLE:-

ROUND ROUND: - returns n, rounded to m places to the right of a decimal point. If m is omitted, n is rounded to 0 places. m can be negative to round off digits to the left to of the decimal point. m must be an integer. Syntax: - ROUND(n,[m])

ROUND EXAMPLE

SQRT FUNCTION SQRT: - Returns square root of n. if n<0, NULL, SQRT returns real result. Syntax: - SQRT(n) Example: -

GREATEST FUNCTION GREATEST: - Returns the greatest value in a list of expression. Syntax: - GREATEST(expr1,expr2,…expn_n) Where expr1, expr2 are expression that are evaluated by the greatest function. Example:

Least function Same as GREATEST function but we will get least value. Same Syntax but we need to use LEAST FUNCTION instead of GREATEST FUNCTION.

MOD FUNCTION MOD : - Returns the remainder of a first number divided by second number passed a parameter. If the second number is zero, the result is the same as first number. Syntax: - MOD(m,n)

MOD EXAMPLE

String function LOWER: - Returns char, with all letters in lowercase. Syntax: - LOWER(char) Example :-

Initcap function INITCAP: - Returns a string with the first letter of each word in upper case. Syntax: - INITCAP(char) Example: -

UPPER FUNCTION UPPER: - Returns char, with all letters forced to uppercase. Syntax: - UPPER(char) Example: -

SUBSTR FUNCTION SUBSTR: - Returns a portion of characters, beginning at character m, and going up to character n. if n is omitted, the result returned is up to last character in the string. The first position of char is 1. Syntax: - SUBSTR(<STRING>,<START_POISTION>,<LENGTH>) Where STRING is the source string. START_POISTION is the position for extraction. The first position in the string is always 1. LENGTH is number of characters to extract

Substr example

Length function LENGTH: - Returns the length of a word. Syntax: - LENGTH(word) Example: -

LTRIM FUNCTION LTRIM: - Removes character from the left of char with initial characters removed upto the first character not in set. Syntax: - LTRIM(char[,set]) Example: -

Question

TRIM FUNCTION TRIM:- Removes all specified characters either from the beginning or the ending of a string. Syntax:- TRIM([leading|trailing|both[<trim_character> from ]] <string1>) Leading:- remove trim_string from the front of string1. Trailing: - remove trim_string from the front and end of string1. Both- remove trim_string from and end of string1

CONVERSION FUNCTION TO_CHAR(number conversion): - converts a values of NUMBER datatype to a character datatype, using the optional format string. To accept a number(n) and a numeric format(fmt) in which the number has to appear. If fmt is omitted, n is converted to a char value exactly long enough to hold all significant digits. Syntax: - TO_CHAR(n[,fmt])

Example to_char(number conversation)

To_char(date conversation) TO_CHAR: - Converts a value of a DATE datatype to CHAR value. TO_CHAR() accepts a data, as well as the format(fmt) in which the date has to appear. Fmt must in date format. If fmt is omitted, the date is converted to a character value using the default date format . What is default format? Syntax: - TO_CHAR(date[,fmt])

Example of to_char

DATE CONVERSION FUNCTION TO_DATE: - Converts a character field to a date field. Syntax: - TO_DATE(char[,fmt]) Example:

DATE FUNTION ADD_MONTHS :- Returns date after adding the number of months specified in the function. Syntax: - ADD_MONTHS(d,n)

ADD_MONTH EXAMPLE

LAST_DAY LAST_DAY : - Returns the last date of the month specified with the function. Syntax : LAST_DAY(d) Example:

MONTHS_BETWEEN MONTHS-BETWEEN: - Returns number of months between d1 and d2. Syntax: - MONTHS_BETWEEN(d1,d2) Example: -

Next-day NEXT_DAY: - Returns the date of first weekday named by char that is after the date named by date. Char must be a day of the week. Syntax: - NEXT_DAY(date,char)

NEXT_DAY FUNCTION

To_char The TO_CHAR function facilitates the retrieval of data in a format different from the default format. It can also extract a part of the date, i.e. the date, month or year from the date value and use it for sorting or grouping of data according to date, month and year.

Syntax TO_CHAR(<date value> [,<fmt>]) where date value stands for the date and fmt is the specified format in which date is to be displayed.

PROBLEM

TO_DATE TO_DATE converts a char value into a date value. It allows a user to insert data into a date column in any required format, by specifying the character value of the date to be inserted and its format

syntax TO_DATE(<char value> [,<fmt>]) Where char value stands for the value to be inserted in the date column, and fmt is date format in which the ‘char value’ is specified

EXAMPLE