[英]Set decimal value to certain value based on underlying number with t-sql case when
在T-Sql中,如何使用大小寫將十進制值設置為基礎數字的基於條件的值? 舉例說明:
列SalesPrice用於價格(小數點后2位),例如129.99或19.99。
我想選擇SalesPrice,但結果稍有調整:
謝謝!
假設所有值都是正值,這是一種方法:
update t
set SalesPrice = (case when SalesPrice >= 100 then floor(SalesPrice) + 1
when SalesPrice < 100 then floor(SalesPrice) + 0.95
end)
where (SalesPrice - floor(SalesPrice)) >= 0.9;
我添加了where
子句,因此這只會影響十進制數大於0.90的價格。 這樣,像15.49
這樣的價格就不會受到影響。 您可以根據需要進行調整。 如果要使用十進制值影響所有價格,則:
update t
set SalesPrice = (case when SalesPrice >= 100 then floor(SalesPrice) + 1
when SalesPrice < 100 then floor(SalesPrice) + 0.95
end)
where SalesPrice > floor(SalesPrice);
where
子句保證有十進制數。 例如,這可以防止21
變成21.95
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.