"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Monday, September 19, 2011

Retrieve custom message if column is null in query

Hi *.*,
 Retrieve custom message if column value is null using query. Oracle, Mysql MSSQL all are providing different keyword for this purpose

NVL() function For Oracle
This function is used to replace NULL value with another value.
Table Sales_Data
store_name
Sales
Store A
300
Store B
NULL
Store C
150







  • SELECT  NVL(Sales,’No data Avilable’) FROM Sales_Data
  • SELECT SUM(NVL(Sales,100)) FROM Sales_Data;
2nd query will returns 550. This is because NULL has been replaced by 100 via the ISNULL function, hence the sum of the 3 rows is 300 + 100 + 150 = 550. 

 ISNULL() function for SQL Server
is used to replace NULL value with another value.

SELECT ISNULL(Sales,’No data avilable’)) FROM Sales_Data;
 IFNULL Function for MySQL
This function takes two arguments. If the first argument is not NULL, the function returns the first argument. Otherwise, the second argument is returned. This function is commonly used to replace NULL value with another value.

SELECT IFNULL(Sales,’No data avilable’) FROM Sales_Data;


Have a nice day... 'N happy Coding :)

No comments: