Monday, March 19, 2012

CASE

Is it possible to use a CASE statement without requiring equality? I don't
want to use a search case, but test a value for match or for range:
CASE @.Integer
WHEN 0 THEN do this
WHEN > 1 do this
WHEN <-1 do that
END
Thanks in advance.try
CASE
WHEN @.Integer = 0 THEN do this
WHEN @.Integer > 1 THEN do this
ELSE do that
END
"R Riness" wrote:

> Is it possible to use a CASE statement without requiring equality? I don't
> want to use a search case, but test a value for match or for range:
> CASE @.Integer
> WHEN 0 THEN do this
> WHEN > 1 do this
> WHEN <-1 do that
> END
> Thanks in advance.
>|||The alternatives for a CASE expression can't be
anything but expressions themselves. Since "do this"
doesn't sound like an expression, this might be more
what's needed (flow control).
Also watching out for the possibility that @.Integer can be
NULL, and avoiding an ELSE that happening more often
than it should:
if @.Integer = 0
begin [do this] end
else if @.Integer > 1
begin [do this] end
else if @.Integer < -1
begin [do this] end
Steve Kass
Drew University
Mike Gemmell wrote:
>try
>CASE
> WHEN @.Integer = 0 THEN do this
> WHEN @.Integer > 1 THEN do this
> ELSE do that
>END
>"R Riness" wrote:
>
>|||Now that I RTFD I know that a searched CASE is not what you want. I am
curious as what you have against a searched case.
If you are attempting something as simple as the example then you could try.
CASE SIGN(@.Integer)
WHEN 0 THEN
WHEN 1 THEN
WHEN -1 THEN
END
to test against a value you could
CASE SIGN(@.Integer - @.Intvalue)
WHEN 0 THEN match_result_expr
WHEN 1 THEN greater_than_result_expr
WHEN -1 THEN less_than_result_expr
END
"Mike Gemmell" wrote:
> try
> CASE
> WHEN @.Integer = 0 THEN do this
> WHEN @.Integer > 1 THEN do this
> ELSE do that
> END
> "R Riness" wrote:
>

No comments:

Post a Comment