开发者

How can I do appear or disappear column according to column value

开发者 https://www.devze.com 2023-02-13 09:40 出处:网络
I am using MSSQL. How can I do appear or disappear a column according to column value? I thought using CASE statement, but It wasnt.开发者_C百科

I am using MSSQL. How can I do appear or disappear a column according to column value?

I thought using CASE statement, but It wasnt.

开发者_C百科

Please help.


Within a single select statement, you can't make a column disappear; the number and name of columns are fixed by the select clause. However, you can overwrite the value of a column in the output, which is where you would typically use a case, nullif, or coalesce expression. For example:

SELECT col1, col2, CASE WHEN col3 = x or col3 = y THEN null ELSE col3 END as col3
  FROM someTable

In this case, col3 is still going to be in the output, but its value will be null if it meets certain conditions specified in the case statement.

If you really don't want the column to be there at all, then you will need to write multiple select statements and choose which one to execute. For example:

declare @checkValue int
select @checkValue = max(col3) from someTable
if (@checkValue = x or @checkValue = y)
    select col1, col2 from someTable
else
    select col2, col2, col3 from someTable

That's an unusual construction. It's really more common to allow the column to appear in the output and simply overwrite its value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号