PL/SQL DataType: Block to differenciate between CHAR and VARCHAR2 datatype

PL/SQL DataType: Exercise-3 with Solution

Write a PL/SQL block to differenciate between CHAR and VARCHAR2 datatype.

Sample Solution:

PL/SQL Code:

  f_name  CHAR(15 CHAR);
  l_name   VARCHAR2(15 CHAR);
  f_name := 'Allen  ';
  l_name  := 'Munra  ';
  DBMS_OUTPUT.PUT_LINE('*' || f_name || '*');
  DBMS_OUTPUT.PUT_LINE('*' || l_name || '*');

Sample Output:

*Allen          *
*Munra  *


Flowchart: PL/SQL DataType - Block to differenciate between CHAR and VARCHAR2 datatype


The both type of variable CHAR and VARCHAR2 have maximum 15 characters long. Both variable have been assigned by a 5 characters long text and trailling by two blank spaces. The value assigned to CHAR variable is padded by 8 blank spaces and two blank spaces was in original data. But the value assigned in VARCHAR2 variable have no changed, it was printed with its two blank spaces which was originally.

Improve this sample solution and post your code through Disqus

Previous: Write a PL/SQL block to insert data to a table using character type variable.
Next:  Write a PL/SQL procedure to accepts a BOOLEAN parameter and uses a CASE statement to print Unknown if the value of the parameter is NULL, Yes if it is TRUE, and No if it is FALSE.

What is the difficulty level of this exercise?

Inviting useful, relevant, well-written and unique guest posts