From Excel to SQL - Conditionals (IIF and CASE)
SQL has an "if" function, just like Excel -- but it has an even better alternative, the CASE statement, for a series of different conditions. The dbfiddle website can be accessed for free and without an account at: - https://dbfiddle.uk/ Make sure to use the SQL Server dialect and the AdventureWorks sample database to be able to follow along with these examples! --------------------------------------------------- To be able to run these examples yourself, you can start with the following SQL code (some of which we'll explain in a later video): ``` SELECT ID, AGE FROM ( VALUES (1, 10), (2, 15), (3, 23), (4, 18), (5, NULL) ) AS AGES(ID, AGE) ``` --------------------------------------------------- This is part of the From Excel to SQL playlist, available at: - https://www.youtube.com/playlist?list=PLEiRgvTilK5rhnVPQ_Tj3Q-CI0rGn_uiD Check out the previous video at: - https://youtu.be/bxZeIkcxYQA ...and the next video at: - https://youtu.be/gYjYLL99jaQ The written version of this content is also available at: - https://billwallis.github.io/sql-learning-materials/from-excel-to-sql/main-concepts/conditionals/ --------------------------------------------------- CHAPTERS 00:00 dbfiddle 00:28 Intro 01:39 Immediate IF 04:37 Accounting for missing data 06:32 The CASE statement 11:37 Step-by-step CASE walkthrough 13:36 Multiple conditions in IIF/CASE 15:26 (Options) Lookup tables for organisation 17:10 Wrap Up
Download
0 formatsNo download links available.