Oracle SQL Where clause

Miracle

I have a query:

select * from students stud
where stud.grade <= :grd 

For example, I entered 80 in :grd, this displays all students that has a grade of 80 and below.

What I wanted is to display all the students if I entered nothing in :grd.

Is there a way to do this? Thanks :)

Popeye

You can simply use coalesce as below

select * from students 
where stud.grade <= coalesce(:grd,stud.grade);

Or use case when as below:

select * from students 
where stud.grade <= 
case when :grd is null 
     then stud.grade 
     else :grd end;

Cheers!!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章