Pages

Showing posts with label PL/SQL CASE. Show all posts
Showing posts with label PL/SQL CASE. Show all posts

How to use PL/SQL NVL?

NVL function is used to handle NULL values in a column.

NVL(columnName, valueIfColumnValueIsNotNull)

Example: Get student list with studentName, examGrade (if it is null put 50 instead)

SELECT studentName, NVL(examGrade, 50) FROM studentGrades


Note: An other way without NVL is to use CASE:


SELECT CASE WHEN examGrade IS NULL THEN 50 ELSE examGrade END FROM studentGrades

How to use PL/SQL CASE?

CASE expression look like switch statements as in programming languages.

Example: Categorize students according to their exam grades.

SELECT 

studentName, 
CASE 
WHEN studentGrade < 50 THEN 'Weak' 
WHEN studentGrade < 90 THEN 'Good'
WHEN studentGrade < 100 THEN 'Well'
END FROM studentGrades