SQL Tutorial - CASE Statements

Поділитися
Вставка
  • Опубліковано 9 лип 2024
  • Another video brought to you by BeardedDev, bringing you tutorials on Business Intelligence, SQL Programming and Data Analysis.
    In this video I talk about how to use CASE statements in SQL Server.
    T-SQL Querying
    www.amazon.com/gp/product/073...
    T-SQL Fundamentals
    www.amazon.com/gp/product/150...
    Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions
    www.amazon.com/gp/product/073...
    CASE Statements were introduced to SQL Server in 2008. They are used to evaluate conditions and return a result based on those conditions. CASE Statements can be used in SELECT, UPDATE, DELETE, WHERE, HAVING.
    In this CASE Tutorial we look at a common use of CASE statements within SELECT and also the benefit and common downfalls when writing CASE queries.
    CASE Statement Syntax:
    CASE WHEN [condition] THEN [result] END
    SQL Queries in this video:
    SELECT
    CustomerId
    , FirstName
    , MiddleName
    , LastName
    , CASE
    WHEN CustomerId = 5995 THEN 'Unknown'
    WHEN Gender = 'M' THEN 'Male'
    WHEN Gender = 'F' THEN 'Female'
    ELSE 'Unknown'
    END AS Gender
    , DOB
    FROM dbo.CustomersSample
    Please feel free to post comments.
  • Наука та технологія

КОМЕНТАРІ • 38

  • @johnbreslin4521
    @johnbreslin4521 3 роки тому +3

    Brilliant, this is exactly what I wanted to do. I can never remember the syntax myself.

  • @malisengjabum5959
    @malisengjabum5959 4 роки тому +5

    The way you explain and write the query is Very Good. You are a great teacher. Thanks.

  • @stealthassulter
    @stealthassulter 4 роки тому +1

    Excellent JOB buddy. Thank you for your time.

  • @doktoren99
    @doktoren99 4 роки тому +1

    Sick video!

  • @mmusciano
    @mmusciano 2 роки тому +1

    Thank you very much

  • @mostinho7
    @mostinho7 3 роки тому +1

    9:45 done

  • @ciruzzi7
    @ciruzzi7 4 роки тому +1

    It's hard for me to find videos that are clear and understandable. As a beginner with SQL, this was spot-on. I will definitely look for more videos from you...and btw luv the accent!

    • @BeardedDevData
      @BeardedDevData  4 роки тому

      That’s great, thanks for the feedback, let me know if there are any areas you would like me to do tutorials on.

    • @ciruzzi7
      @ciruzzi7 4 роки тому

      @@BeardedDevData how about SSRS reports?

  • @alexkoh4045
    @alexkoh4045 4 роки тому +1

    THANK YOU SO MUCH

  • @enjaku2600
    @enjaku2600 5 років тому +1

    Thanks mate. Very clear and useful. Much appreciated. -eric

    • @BeardedDevData
      @BeardedDevData  5 років тому

      No problem, glad you found the video useful.

  • @somalipirates
    @somalipirates 4 роки тому +1

    AMAZING. thank you.. keep them coming

  • @hemantbhatt1486
    @hemantbhatt1486 3 роки тому +1

    Good explanation

  • @leticialima7123
    @leticialima7123 4 роки тому +1

    Hi, Can you submit an the test database to we ?

  • @harrydadson7467
    @harrydadson7467 3 роки тому +1

    This is very good playlist, Thank you very much...only criticism I have is I wished it was is chronological order :)

    • @BeardedDevData
      @BeardedDevData  3 роки тому +1

      I appreciate that, I do need to sort out the playlists.

  • @WizOWash
    @WizOWash 4 роки тому +2

    Thanks so much for this tutorial! Excellently done. I did have a question about the line you added for the CustomerID when you set the Gender value to Unknown. How does SQL know to set the Gender value to NULL. From the statement WHEN CustomerID = 5995 THEN 'Uknown'. It does not reference the Gender field at all in that line. Thanks

    • @BeardedDevData
      @BeardedDevData  4 роки тому +4

      I am effectively overwriting the Gender column within the SELECT, as you can see in the CASE statement, “END AS Gender”, I don’t need to actually reference the column to set a value.

    • @AZ_German
      @AZ_German 2 роки тому

      Had the same question. Thanks for asking.

  • @vru5696
    @vru5696 2 роки тому +1

    how to write case statements in where clause to check dates with between logic. for eg. startdate between table1.startdate and table2.enddate

    • @BeardedDevData
      @BeardedDevData  2 роки тому

      You can just use WHERE startdate BETWEEN table1.startdate AND table2.enddate, there is no need for a case statement

    • @vru5696
      @vru5696 2 роки тому

      @@BeardedDevData actually based on nulls record in date need to use case

  • @QloweeD
    @QloweeD 5 років тому +1

    Hi, can you help me right up a case when statement for the example below,
    Say case when count is 4 then a=20 and rest of variables in the group (b,c,d) = 10.
    I want the statement to assign 20 to "a" and assign 10 to each of the remaining variable.

    • @BeardedDevData
      @BeardedDevData  5 років тому

      Can you give me more details, it sounds like you are initially aggregating the values?

  • @siddhipore1715
    @siddhipore1715 2 роки тому +1

    How to write a case statment to reject those records which has null values in gender column?

    • @BeardedDevData
      @BeardedDevData  2 роки тому +1

      You wouldn't want to use a case statement in this scenario, instead use WHERE gender IS NOT NULL, this will filter NULLs in the gender column. If you want to change what is returned then use a case statement CASE WHEN gender IS NULL THEN 'Unknown' END, case statements will not apply filtering.

    • @siddhipore1715
      @siddhipore1715 2 роки тому +1

      @@BeardedDevData thanks for the reply

  • @sairam8406
    @sairam8406 3 роки тому +1

    thumb's up

  • @useless3905
    @useless3905 2 роки тому +1

    Thanks sir can you do this with if else statement?

    • @BeardedDevData
      @BeardedDevData  2 роки тому

      An IF ELSE statement is used to control logic, you can use an IIF function to replace simple CASE statements, the main difference is that with IIF you can return one of two value but with CASE statements you can return one of many values.

  • @azzamf.khayyat8072
    @azzamf.khayyat8072 2 роки тому +1

    Thanks for making the video. What application do u recommend for writing queries in mobile? --WHERE mobile IN ( 'apple' , 'android' );

    • @BeardedDevData
      @BeardedDevData  2 роки тому

      To be honest I have never tried to run queries on a mobile platform, I can see the benefit from a training point of view but don't think it would be useful in production, I would be curious to know if you find a way to do it though.

  • @AlishSafarli
    @AlishSafarli 4 роки тому +1

    All the case statements I have seen are about gender column lol set 'm' to 'something'
    is there another advanced query you can show ?

    • @BeardedDevData
      @BeardedDevData  4 роки тому +2

      Sure, you are right the examples are quite straight forward and case statements are quite an interesting topic. I will put some videos up showing applying ranges using multiple conditions, using built in functions inside case statements and building a dynamic where clause with case statements.

    • @AlishSafarli
      @AlishSafarli 4 роки тому

      BeardedDev that would be great bro! What is your programming level?senior?

  • @ondaride777
    @ondaride777 2 роки тому +1

    For genders using multiples instead of just 2 makes it even more clear and politically correct lol

  • @Gigatless
    @Gigatless 4 роки тому +2

    One can only imagine how many sjw were triggered by this tutorial