对于amountB为null或0的测试用例,选择amountA的最简单方法是什么?
DECLARE @amountA float;
DECLARE @amountB float = 3.33;
select coalesce(@amountA, @amountB)
SET @amountA = 0.00;
select coalesce(@amountA, @amountB)发布于 2020-04-13 19:52:46
在这种情况下,我将使用CASE
select case when @amountA is null or @amountA = 0 then @amountB else @amountA end发布于 2020-04-13 19:56:15
你只是在找
DECLARE @amountA float;
DECLARE @amountB float = 3.33;
select coalesce(nullif(@amountA, 0), @amountB);
SET @amountA = 0.00;
select coalesce(nullif(@amountA, 0), @amountB);或者像我在评论中所说的那样使用CASE表达式或IIF()函数。
发布于 2020-04-13 20:04:36
您可以将case嵌套在coalesce中。
select coalesce(case when @amountA > 0 then @amountA end, @amountB)也可以重写为
select case when @amountA > 0 then @amountA else @amountB end请注意,除非您将上面的条件运算符从>更改为<>,否则负值将被分配给@工程量B。
https://stackoverflow.com/questions/61195574
复制相似问题