How to use IF condition in SQL SELECT statement in Snowflake
- How-Tos FAQs
- September 1, 2021
In SQL Server, we have below 3 options to implement IF condition in SQL SELECT statement in Snowflake
- An easy-peasy no-code method using Datameer 😁.
- CASE WHEN… THEN
Let us consider the following ‘student’ table.
CREATE TABLE student ( student_name VARCHAR(50), grade CHAR(1) ) INSERT INTO student SELECT 'Jack', 'A' UNION ALL SELECT 'Rose', 'B' UNION ALL SELECT 'John', 'C' UNION ALL SELECT 'Dave', 'D' UNION ALL SELECT 'Chris', 'F'
1. An easy-peasy no-code method using Datameer 😁.
In this video you can see how to use IF condition in SQL SELECT statement in Snowflake with Datameer
Datameer is a SaaS data transformation tool that takes the coding out of SQL coding.
With Datameer (on Snowflake), you don’t have to worry about memorizing the proper “case” syntax.
With our low-code, no-code, and code approach, we have what it takes to cater to different data personnel and personas.
Whether you’re an SQL expert, a beginner, or an intermediate data analyst, we have the correct interface to assist you on your data modeling and transformation journey.
Regarding conditional expressions, we support advanced conditionals such as CASE, COALESCE, GREATEST, IFF, NULLIF, NVL, NVL2, etc.
Go ahead and take Datameer for a spin with our free 14-day trial!
2. CASE WHEN… THEN
We can use this option when we have multiple cases to be handled.
SELECT student_name 'Student Name', CASE grade WHEN 'A' THEN 'Excellent' WHEN 'B' THEN 'Good' WHEN 'C' THEN 'Average' WHEN 'D' THEN 'Poor' WHEN 'F' THEN 'Fail' ELSE 'N/A' END 'Grade' FROM student -- Output Student Name Grade -------------------------- Jack Excellent Rose Good John Average Dave Poor Chris Fail
From SQL SERVER 2012, we have the option to use IIF statements. It is like a Shorthand form of CASE statement. We can conveniently use it when we need to decide between two options. There are three parts in IIF statement, first is a condition, second is a value if the condition is true and the last part is a value if the condition is false.
SELECT student_name 'Student Name', IIF(grade = 'f', 'Fail', 'Pass') 'Pass/Fail' FROM student -- Output Student Name Pass/Fail --------------------------------- Jack Pass Rose Pass John Pass Dave Pass Chris Fail
Read How to create an AUTO_INCREMENT column in a table on Oracle DB using SQL?