簡體   English   中英

當使用t-sql時,根據基礎數字將十進制值設置為特定值

[英]Set decimal value to certain value based on underlying number with t-sql case when

在T-Sql中,如何使用大小寫將十進制值設置為基礎數字的基於條件的值? 舉例說明:

SalesPrice用於價格(小數點后2位),例如129.99或19.99。

我想選擇SalesPrice,但結果稍有調整:

  • 如果SalesPrice> = 100,則小數應為.00(129.99-> 130.00)
  • 如果SalesPrice <100,則小數應為.95(19.99-> 19.95)

謝謝!

假設所有值都是正值,這是一種方法:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM