SQL 我想将负数设置为零,任何正数都应保持不变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6585160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:13:20  来源:igfitidea点击:

I want to set negative numbers in a view to zero and any positive should remain the same

sqlcase

提问by user829982

select field1, field2, Field3,
case when exists
(select field1
from tbl1
where field < 0) then 0 else field1 end as Field2
from tbl1

回答by CatchingMonkey

Assuming its T-SQL, its similar to a question from earlier but here goes:

假设它的 T-SQL,它类似于之前的一个问题,但这里是:

SELECT [your fields here] ,
CASE WHEN [COLUMN_NAME] <= 0 THEN 0 ELSE [COLUMN_NAME]  END AS [NEW_COL_NAME]
FROM [TABLE_NAME]

回答by Utoxin

Use this snippet in the query you create the view from:

在您创建视图的查询中使用此代码段:

IF(field < 0, 0, field)

回答by Chandu

Try this:

尝试这个:

SELECT field1, 
    field2, 
    Field3, 
    CASE 
        WHEN  field < 0  THEN 0 
        ELSE field1 
    END as Field2 
    FROM tbl1 

回答by pratik garg

you can do like follow-

你可以像跟随-

select 
case
when field1<0 then 0
else field1
end as field1
from tbl