SQL Server中的条件值替换

时间:2021-09-10 11:49:20

I have a table that has several columns. The value of one column is 0 or 1. I want to write a query that returns "Hello" if the value was 0, or "Bye" if it was 1. What is the appropriate way to write this query?

我有一个有几列的表。一列的值是0或1.我想编写一个查询,如果值为0则返回“Hello”,如果是1,则返回“Bye”。编写此查询的适当方法是什么?

2 个解决方案

#1


13  

Use a CASE expression

使用CASE表达式

SELECT CASE YourCol
         WHEN 0 THEN 'Hello'
         WHEN 1 THEN 'Bye'
       END AS SomeAlias
FROM   YourTable  

#2


1  

If you choose multi/all columns, please try with below:

如果您选择多列/所有列,请尝试使用以下内容:

SELECT Column1, Column2,  -- Put other column name here 
        CASE TargetColumnName
         WHEN 0 THEN 'Hello'
         WHEN 1 THEN 'Bye'
       END AS TargetAliasColumnName
FROM   YourTableName 

#1


13  

Use a CASE expression

使用CASE表达式

SELECT CASE YourCol
         WHEN 0 THEN 'Hello'
         WHEN 1 THEN 'Bye'
       END AS SomeAlias
FROM   YourTable  

#2


1  

If you choose multi/all columns, please try with below:

如果您选择多列/所有列,请尝试使用以下内容:

SELECT Column1, Column2,  -- Put other column name here 
        CASE TargetColumnName
         WHEN 0 THEN 'Hello'
         WHEN 1 THEN 'Bye'
       END AS TargetAliasColumnName
FROM   YourTableName