Sql 创建包含if-else的视图
我想查看或只是从数据库中获取一些数据,但具体方式是 例如,如果数据为:Sql 创建包含if-else的视图,sql,sql-server,Sql,Sql Server,我想查看或只是从数据库中获取一些数据,但具体方式是 例如,如果数据为: 1 | test | test | 0 | test 2 | test | test | 1 | test 3 | test | test | 1 | test 4 | test | test | 1 | test 5 | test | test | 0 | test 输出应为: 1 | test | test | FALSE | test 2 | test | test | TRUE | test
1 | test | test | 0 | test
2 | test | test | 1 | test
3 | test | test | 1 | test
4 | test | test | 1 | test
5 | test | test | 0 | test
输出应为:
1 | test | test | FALSE | test
2 | test | test | TRUE | test
3 | test | test | TRUE | test
4 | test | test | TRUE | test
5 | test | test | FALSE | test
有什么想法吗?如果SQL中的条件使用以下表达式表示,则为
:
请注意,SQL Server中有两种形式的CASE
表达式-简单表达式和搜索表达式。上面是一个简单的。使用表达式的示例
查询
SELECT col1, col2, col3,
CASE col4 WHEN 0 THEN 'False'
WHEN 1 THEN 'TRUE'
ELSE NULL END AS col4, col5
FROM your_table_name;
我想你可能在找一份案情陈述书。试试这个:
SELECT
Col1,
Col2,
Col3,
CASE WHEN Col4 = 0 THEN 'False' WHEN Col4 = 1 Then 'True' ELSE NULL END,
Col5
FROM YourTableName
If 2012+您可以使用内联If
IIF(condition,a,b)
IIF(condition,a,b)