PLSQL Variables - Datatype Conversion and Operators
PLSQL Variables - Datatype Conversion and Operators
•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
• 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
• 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