I have percentages in a condition
table:
create table condition (percent_decimal number(3,2));
insert into condition values (-0.01);
insert into condition values (0.1);
insert into condition values (1);
insert into condition values (1.1);
commit;
PERCENT_DECIMAL
---------------
-0.01
.1
1
1.1
I want to select the values, but modify them to present them as percentages between 0 and 1 (inclusive):
- Convert
-0.01
to0
- Leave
.1
as is - Leave
1
as is - Convert
1.1
to1
I can successfully do this using the greatest and least functions:
select
percent_decimal,
least(1,greatest(0,percent_decimal)) as percent_modified
from
condition
PERCENT_DECIMAL PERCENT_MODIFIED
--------------- ----------------
-0.01 0
.1 .1
1 1
1.1 1
However, I'm wondering if there is a more succinct way of doing this–with a single function.
I'm often pleasantly surprised by the clever techniques that DBA-SE members come up with. When I think I've come up with the simplest way to do something, someone comes up with something even simpler.
Best Answer
Ok, this can't be a serious question, reminds me of https://codegolf.stackexchange.com/
I can do better, no function at all.
But we need a function. Finally Oracle 12.2 allows long (128 bytes) identifiers, so we can give more meaningful names:
I also happen to have a super secret function that coincidentally does exactly what you need:
Must use a built-in function? No problem.