Hello friends I have been using this query to select some two fields on my Java application.
select distinct STU_LEVEL, STU_STREAM from STUDENT_MASTER_TABLE where STATUS_YEAR !=ACADEMIC_YEAR
Problem is if there is no data, the application is throwing a null pointer Exception. I need it such that if the fields are Null, then they are defaulted to 0. I have tried this with Coalesce but I can’t seem to get it like:
select coalesce(distinct (STU_LEVEL,0), (STU_STREAM, '0')) from STUDENT_MASTER_TABLE where STATUS_YEAR !=ACADEMIC_YEAR
Please help.
You just need to use
COALESCEtwice like this:This will return 0 for STU_Level if it is NULL and 0 for STU_Stream if it is NULL.
SQL Fiddle Demo
In response to your comment, it looks like you need to use a UNION get your desired results when no records are found:
And a sample fiddle: http://www.sqlfiddle.com/#!4/29039/1