I put two parameters in a query; [A_from] and [A_to] to decide some value in table for data set.
The both of parameters allow Null/empty and the value changes by parameter as you can see below. How can I write query for this? (Tried case statement, but doesn't work..)
Input) Output)
A_From and A_To some Value Between A_From And A_To
Only A_From some value >= A_From
Only A_To some value <= A_To
Both NULL * (all)
Since comparisons to null
always yield false
, you can use this:
select *
from my_table t
where
(t.value >= A_From and t.value <= A_To) or
(t.value >= A_From and A_To is null) or
(A_From is null and t.value <= A_To) or
(A_From is null and A_to is null)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.