SQL Tutorial - CASE Statements

preview_player
Показать описание
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

T-SQL Fundamentals

Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions

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.
Рекомендации по теме
Комментарии
Автор

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

johnbreslin
Автор

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

malisengjabum
Автор

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!

ciruzzi
Автор

Excellent JOB buddy. Thank you for your time.

stealthassulter
Автор

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

BigHny
Автор

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

WizOWash
Автор

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

azzamf.khayyat
Автор

Thanks sir can you do this with if else statement?

useless
Автор

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

enjaku
Автор

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

leticialima
Автор

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

vru
Автор

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

AlishSafarli
Автор

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

siddhipore
Автор

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.

QloweeD
Автор

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

ondaride
Автор

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

Gigatless
visit shbcf.ru