Data type conversion, PL-SQL Programming

Assignment Help:

Datatype Conversion

At times it is necessary to convert a value from one datatype to another. For e.g. if you want to inspect a rowid, you should convert it to a character string. The PL/SQL supports both the explicit and implicit (automatic) datatype conversion.

Explicit Conversion

To convert the values from one datatype to other, you use the built-in functions. For e.g. to convert a CHAR  value to a DATE   or NUMBER   value, use the function TO_DATE  or TO_NUMBER, respectively. On the contrary, to convert a DATE or NUMBER value to the CHAR value, you use the function TO_CHAR.

Implicit Conversion

When it makes sense, the PL/SQL can convert the datatype of a value implicitly. This permits you to use the variables, literals, and parameters of one type where the other type is expected. In the example shown below, the CHAR variables start_time and finish_ time hold string values representing the number of seconds in the past midnight. The variation between those values must be assigned to the NUMBER variable elapsed_time. And hence, the PL/SQL converts the CHAR values to the NUMBER values automatically.

DECLARE

start_time CHAR(5);

finish_time CHAR(5);

elapsed_time NUMBER(5);

BEGIN

/* Get system time as seconds past midnight. */

SELECT TO_CHAR(SYSDATE,'SSSSS') INTO start_time FROM sys.dual;

-- do something

/* Get system time again. */

SELECT TO_CHAR(SYSDATE,'SSSSS') INTO finish_time FROM sys.dual;

/* Compute elapsed time in seconds. */

elapsed_time := finish_time - start_time;

INSERT INTO results VALUES (elapsed_time, ...);

END;

Before assigning a selected column value to a variable, the PL/SQL will, if necessary then convert the value from the datatype of the source column to the datatype of the variable. This happens, for e.g.  If you select a DATE column value into a VARCHAR2 variable.

Similarly, before assigning the value of a variable to a database column, the PL/SQL will, if necessary then convert the value from the datatype of the variable to the datatype of the target column. If the PL/SQL cannot determine that implicit conversion is required, you get a compilation error. In such situation, you should use a datatype conversion function. The table shows that implicit conversions PL/SQL can do.

566_data type conversion.png

It is your duty to ensure that the values are convertible. For illustration, the PL/SQL can convert the CHAR value '02-JUN-92' to a DATE value but cannot convert the CHAR value 'YESTERDAY' to a DATE value. Likewise, the PL/SQL cannot convert a VARCHAR2 value containing the alphabetic characters to a NUMBER value.

Implicit versus Explicit Conversion

Normally, to rely on the implicit datatype conversions is a poor programming practice as they can hamper the performance and might change from one software release to the next. Also, the implicit conversions are context sensitive and hence not always predictable. Rather, use datatype conversion functions. In that way, your applications will be easier and reliable to maintain.


Related Discussions:- Data type conversion

Keyword and parameter description - forall statement, Keyword &Parameter De...

Keyword &Parameter Description: index_name: This is an undeclared identifier which can be referenced only within the FORALL statement and only as the collection subscript

Transaction control, Transaction Control The Oracle is transaction orie...

Transaction Control The Oracle is transaction oriented; that is, Oracle uses the transactions to make sure the data integrity. The transaction is a sequence of SQL data manip

Ending transactions, Ending Transactions A good quality programming pr...

Ending Transactions A good quality programming practice is to commit or roll back every transaction explicitly. Whether you rollback or issue the commit in your PL/SQL program

Lob types in pl/sql, LOB Types The large object (LOB) datatypes like BF...

LOB Types The large object (LOB) datatypes like BFILE, BLOB, CLOB, and NCLOB store the blocks of unstructured data (like graphic images, text, video clips, and sound waveforms)

Rewrite rules - tautology, Equivalences & Rewrite Rules: If notice tha...

Equivalences & Rewrite Rules: If notice that as well as allowing us to prove trivial theorems, and tautologies enable us to establish that certain sentences are saying the sam

Case sensitivity-naming conventions, Case Sensitivity Similar to all the...

Case Sensitivity Similar to all the identifiers, the variables, the names of constants, and parameters are not case sensitive. For illustration, PL/SQL considers the following n

I want database development with analysis tools, Project Description: I ...

Project Description: I want a database for large governmental and private data sets on one country that will be easily extended to other countries in the future. Also, the datab

Parameter and keyword description - update statement, Parameter and Keyword...

Parameter and Keyword Description:   table_reference: This keyword identifies the table or view that should be accessible when you execute the UPDATE statement, and for wh

Implicit cursors, Implicit Cursors The Oracle implicitly opens a curso...

Implicit Cursors The Oracle implicitly opens a cursor to process each SQL statement not related with an explicitly declared cursor. The PL/SQL lets you refer to the most recen

Sql pseudocolumns, SQL Pseudocolumns The PL/SQL recognizes the followin...

SQL Pseudocolumns The PL/SQL recognizes the following SQL pseudocolumns, that returns the specific data items: LEVEL, NEXTVAL, CURRVAL, ROWID, & ROWNUM. The Pseudocolumns are n

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd