0% found this document useful (0 votes)
12 views

PLSQL Variables - Datatype Conversion and Operators

The document provides an overview of PL/SQL variables, datatype conversion, and operators. It covers the declaration and characteristics of variables, the process of implicit and explicit datatype conversion, and the use of specific conversion functions such as TO_CHAR, TO_NUMBER, and TO_DATE. Additionally, it categorizes PL/SQL operators into arithmetic, relational, comparison, and logical types.

Uploaded by

ssuresh2107
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

PLSQL Variables - Datatype Conversion and Operators

The document provides an overview of PL/SQL variables, datatype conversion, and operators. It covers the declaration and characteristics of variables, the process of implicit and explicit datatype conversion, and the use of specific conversion functions such as TO_CHAR, TO_NUMBER, and TO_DATE. Additionally, it categorizes PL/SQL operators into arithmetic, relational, comparison, and logical types.

Uploaded by

ssuresh2107
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

PL/SQL Datatype Conversion, Operators

Topics PLSQL – Variables - INTRODUCTION


- Quick Recap on Variables & Datatypes
- Datatype Conversion
- PL/SQL Operators
- Q&A
PL/SQL – Variables Recap

•A variable can be declared only in the declarative part of any block, subprogram, or package
•It should start with an Alphabet or _ and consist of Alphabet, Number, # $ & _
•It cannot be PLSQL or SQL Keywords
•We can anchor variables to the data type of table column using %TYPE
•We can create variable to hold entire row of a table using %ROWTYPE
•Variables are visible inside the block in which they are declared and also in subblocks
•Frequently used data types

• Varchar2 has a max limit of 4000 chars, to contain more, use the CLOB data type
•Use BLOB to store image/raw binary data
•Record data type is a composite data type used to store set of columns in one variable
•Example of record datatype
PL/SQL – Datatype Conversion

•Data type conversion happens when specific data type is expected, however data of another datatype is
passed
•Data type conversion happens both Implicitly and Explicitly in Oracle PL/SQL
•Type conversion can be either implicitly done by Oracle or explicitly done by the programmer.
•Oracle does implicit datatype conversion based on a matrix (Detailed in next slide)
•Oracle offers type conversion functions which can be used in the queries for explicit conversion and
formatting.
•it is recommended to perform explicit conversion instead of relying on software intelligence.
PL/SQL Variables – Datatype Conversion Matrix

• Whenever PL/SQL detects that a conversion is necessary, it attempts to change the values
as required to perform the operation
PL/SQL Datatype Conversion – Implicit Conversion

Implicit Data Type Conversion


• A VARCHAR2 or CHAR value can be implicitly converted to NUMBER or DATE type value
• Similarly, a NUMBER or DATA type value can be automatically converted to character data
• implicit interconversion happens only when the character represents a valid number or
date type value respectively
PL/SQL Datatype Conversion – Explicit Conversion

Explicit Data Type Conversion


• SQL Conversion functions are single row functions which are capable of typecasting column value, literal
or an expression
•TO_CHAR, TO_NUMBER and TO_DATE are the three functions which perform cross modification of data
types.
• TO_CHAR function is used to typecast a numeric or date input to character type with a format model
(optional).
• TO_NUMBER function converts a character value to a numeric datatype. If the string being converted
contains nonnumeric characters, the function returns an error.
• To_DATE function takes character values as input and returns formatted date equivalent of the same
PL/SQL Datatype Conversion – TO_CHAR

• TO_CHAR can be used to convert a number or date to character type


PL/SQL Datatype Conversion – TO_CHAR
Format Models
PL/SQL Datatype Conversion – TO_CHAR
Format Models
PL/SQL Datatype Conversion – TO_NUMBER

• TO_NUMBER lets you convert a string (VARCHAR2, CHAR, etc) to a NUMBER type.
• If the string being converted contains nonnumeric characters, the function returns an error.
• We can use this if, we are using the value in any expression

• Another important use is to validate if the value present in a non-numeric datatype variable is all numbers
E.g. Value got from a flat file need to be validated before using it any expression of DML
PL/SQL Datatype Conversion – TO_DATE

• The function takes character values as input and returns formatted date equivalent of the same.

• TO_DATE function allows users to enter a date in any format, and then it converts the entry into
the default format used by Oracle 11g.

• A format_mask argument consists of a series of elements representing exactly what the data should look
like and must be entered in single quotation marks.
PL/SQL Datatype Conversion – TO_DATE
Format Models
PL/SQL Datatype Conversion – TO_DATE
Format Models
PL/SQL Operators

• An operator is a symbol that tells the compiler to perform the operation on one or more operands

• In PL/SQL, operators can be classified broadly into following categories:

• Arithmetic operators

• Relational operators

• Comparision operators

• Logical operators
PL/SQL Arithmetic Operators
PL/SQL Relational Opetators
PL/SQL Logical Opetators
PL/SQL Comparison Operators
PL/SQL Variables

Q&A

You might also like