Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a variable is null in plsql

Tags:

sql

oracle

plsql

I want to check if a variable is null. If it is null, then I want to set a value to that variable:

//data type of var is  number  if Var = null then   var :=5; endif 

But I am geting error in it. How can I check if a variable is null?

I am using oracle data type

like image 788
user223541 Avatar asked Dec 31 '09 06:12

user223541


People also ask

How do I check if a variable is null in PLSQL?

It takes any number of arguments, and returns the first one which is not NULL. If all the arguments passed to COALESCE are NULL, it returns NULL. In contrast to NVL , COALESCE only evaluates arguments if it must, while NVL evaluates both of its arguments and then determines if the first one is NULL, etc.

What is not null variable in PLSQL?

NOT NULL. This constraint prevents the assigning of nulls to a variable or constant. At run time, trying to assign a null to a variable defined as NOT NULL raises the predefined exception VALUE_ERROR . The constraint NOT NULL must be followed by an initialization clause.

IS null check in Oracle?

Oracle IS NOT NULL operator The operator IS NOT NULL returns true if the expression or value in the column is not null. Otherwise, it returns false.

Can I use NVL in PLSQL?

The NVL function can be used in the following versions of Oracle/PLSQL: Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i.


2 Answers

if var is NULL then   var :=5; end if; 
like image 87
Priyank Avatar answered Sep 25 '22 07:09

Priyank


Use:

IF Var IS NULL THEN   var := 5; END IF; 

Oracle 9i+:

var = COALESCE(Var, 5) 

Other alternatives:

var = NVL(var, 5) 

Reference:

  • COALESCE
  • NVL
  • NVL2
like image 27
OMG Ponies Avatar answered Sep 23 '22 07:09

OMG Ponies