Conditional value replacement in SQL Server
-
13-03-2021 - |
سؤال
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?
المحلول
Use a CASE
expression
SELECT CASE YourCol
WHEN 0 THEN 'Hello'
WHEN 1 THEN 'Bye'
END AS SomeAlias
FROM YourTable
نصائح أخرى
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
لا تنتمي إلى StackOverflow