amazon-redshiftHow do I use the CASE WHEN statement in Amazon Redshift?
The CASE WHEN statement is used to evaluate a set of conditions and return a result. It is similar to an IF-THEN-ELSE statement. In Amazon Redshift, the CASE WHEN statement is used to evaluate a set of conditions and return a result based on the conditions that are met.
For example, the following code will return either 'Pass' or 'Fail' depending on the value of the 'grade' column:
SELECT CASE WHEN grade >= 50 THEN 'Pass' ELSE 'Fail' END AS result
FROM table_name
The code consists of the following parts:
- The
CASE
keyword, which starts the statement. - The
WHEN
keyword, which is followed by a condition. - The
THEN
keyword, which is followed by the result if the condition is met. - The
ELSE
keyword, which is followed by the result if the condition is not met. - The
END
keyword, which ends the statement.
For more information about the CASE WHEN statement in Amazon Redshift, please refer to the documentation.
More of Amazon Redshift
- How can I monitor Amazon RDS using Zabbix?
- How can I handle divide by zero errors when using Amazon Redshift?
- How do I use the Amazon Redshift YEAR function?
- How can I calculate the serverless pricing for Amazon Redshift?
- How do I use Amazon Redshift RSQL to query data?
- How do I use Amazon Redshift window functions?
- How do I use Amazon Redshift to store data in an S3 bucket?
- How do I replace a table in Amazon Redshift?
- How can I use Amazon Redshift external tables?
- How do I use regular expressions with Amazon Redshift?
See more codes...