Conversion Functions Implicit datatype conversion Explicit datatype conversion Datatypeconversion In some cases, Oracle Server allows data of one datatype.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
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.
2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
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.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
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,
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.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
CH3 Part1 Single-Row Functions
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.
After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.
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.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
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.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
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.
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.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
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.
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
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.
Copyright  Oracle Corporation, All rights reserved. 8 Producing Readable Output with SQL*Plus.
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.
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.
Defining a Column Alias
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
4 Copyright © 2009, Oracle. All rights reserved. Using Conversion Functions and Conditional Expressions.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
RELATSIOONILISED ANDMEBAASID (algus , ORACLE materjalid)
Ch. 3 Single-Row Functions
Single-Row Functions Schedule: Timing Topic 55 minutes Lecture
Subqueries.
(SQL) Single-Row Functions
Writing Basic SQL Statements
Review SQL Advanced.
Restricting and Sorting Data
Presentation transcript:

Conversion Functions Implicit datatype conversion Explicit datatype conversion Datatypeconversion In some cases, Oracle Server allows data of one datatype where it expects data of a different datatype. This is allowed when Oracle Server can automatically convert the data to the expected datatype. This datatype conversion can be done implicitly by Oracle Server or explicitly by the user.

Explicit Datatype Conversion NUMBERCHARACTER TO_CHARTO_NUMBER DATETO_CHAR TO_DATE

TO_CHAR Function with Dates The format model: Must be enclosed in single quotation marks and is case sensitive Can include any valid date format element Has an fm element to remove padded blanks or suppress leading zeros Is separated from the date value by a comma The format model: Must be enclosed in single quotation marks and is case sensitive Can include any valid date format element Has an fm element to remove padded blanks or suppress leading zeros Is separated from the date value by a comma TO_CHAR(date, 'fmt')

Elements of Date Format Model Time elements format the time portion of the date. Add character strings by enclosing them in double quotation marks. Number suffixes spell out numbers. Time elements format the time portion of the date. Add character strings by enclosing them in double quotation marks. Number suffixes spell out numbers. HH24:MI:SS AM15:45:32 PM DD "of" MONTH12 of OCTOBERddspthfourteenth

Using TO_CHAR Function with Dates SQL> SELECTename, 2 TO_CHAR(hiredate, 'fmDD Month YYYY') HIREDATE 3 FROM emp; ENAME HIREDATE KING 17 November 1981 BLAKE 1 May 1981 CLARK 9 June 1981 JONES 2 April 1981 MARTIN 28 September 1981 ALLEN 20 February rows selected.

NVL Function Converts null to an actual value Datatypes that can be used are date, character, and number. Datatypes must match NVL(comm,0) NVL(hiredate,'01-JAN-97') NVL(job,'No Job Yet') Converts null to an actual value Datatypes that can be used are date, character, and number. Datatypes must match NVL(comm,0) NVL(hiredate,'01-JAN-97') NVL(job,'No Job Yet')

Syntax: NVL (expr1, expr2) where: expr1 is the source value or expression that may contain null expr2 is the target value for converting null You can use the NVL function to convert any datatype, but the return value is always the same as the datatype of expr1. NVL Conversions for Various Datatypes DatatypeConversion Example NUMBERNVL (number_column.9) DATENVL (date_column, ’01-JAN-95’) CHAR or VARCHAR2NVL(character_column, ‘Unavailable’)

SQL> SELECT ename, sal, comm, (sal*12)+NVL(comm,0) 2 FROM emp; Using the NVL Function ENAME SAL COMM (SAL*12)+NVL(COMM,0) KING BLAKE CLARK JONES MARTIN ALLEN rows selected.

DECODE Function Facilitates conditional inquiries by doing the work of a CASE or IF- THEN-ELSE statement DECODE(col/expression, search1, result1 [, search2, result2,...,] [, default]) DECODE(col/expression, search1, result1 [, search2, result2,...,] [, default])

Using the DECODE Function SQL> SELECT job, sal, 2 DECODE(job, 'ANALYST', SAL*1.1, 3 'CLERK', SAL*1.15, 4 'MANAGER', SAL*1.20, 5 SAL) 6 REVISED_SALARY 7 FROM emp; JOB SAL REVISED_SALARY PRESIDENT MANAGER MANAGER rows selected.

Using the DECODE Function In the SQL statement above, the value of JOB is decoded. If JOB is ANALYST, the salary increase is 10%; if JOB is CLERK, the salary increase is 15%; if JOB is MANAGER, the salary increase is 20%. For all other job roles, there is no increase in salary. The same statement can be written as an IF-THEN-ELSE statement: IF job = 'ANALYST'THEN sal = sal*1.1 IF job = 'CLERK' THEN sal = sal*1.15 IF job = 'MANAGER'THEN sal = sal*1.20 ELSE sal = sal

Using the DECODE Function SQL> SELECT ename, sal, 2 DECODE(TRUNC(sal/1000, 0), 3 0, 0.00, 4 1, 0.09, 5 2, 0.20, 6 3, 0.30, 7 4, 0.40, 8 5, 0.42, 9 6, 0.44, ) TAX_RATE 11 FROM emp 12 WHERE deptno = 30; Display the applicable tax rate for each employee in department 30.

Nesting Functions Single-row functions can be nested to any level. Nested functions are evaluated from deepest level to the least-deep level. Single-row functions can be nested to any level. Nested functions are evaluated from deepest level to the least-deep level. F3(F2(F1(col,arg1),arg2),arg3) Step 1 = Result 1 Step 2 = Result 2 Step 3 = Result 3

Nesting Functions SQL> SELECTename, 2 NVL(TO_CHAR(mgr),'No Manager') 3 FROMemp 4 WHEREmgr IS NULL; ENAME NVL(TO_CHAR(MGR),'NOMANAGER') KING No Manager