Search
 
SCRIPT & CODE EXAMPLE
 

SQL

SQL CASE With ELSE in SQL

SELECT customer_id, first_name,
CASE
    WHEN country = 'USA' THEN 'United States of America'
    WHEN country = 'UK' THEN 'United Kingdom'
    ELSE 'Unknown Country'
END AS country_name
FROM Customers;
Comment

SQL CASE Statement

SELECT order_id, customer_id,
CASE
    WHEN amount >= 400 THEN (amount - amount * 10/100)
END AS offer_price
FROM Orders;
Comment

case statement in sql

Case Statement basically
Like IF - THEN - ELSE statement.

The CASE statement goes through conditions
and returns a value when the
first condition is met and
once a condition is true,
it will stop reading and return the result.
If no conditions are true,
it returns the value in the ELSE clause.

If there is no ELSE part and
no conditions are true, it returns NULL.

FOR EXAMPLE =

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END 

-- example:
SELECT 
	CASE
      WHEN (1+6 = 6) THEN 'A'
      WHEN (1+6 = 7) THEN 'B'
      WHEN (1+6 = 8) THEN 'C'
      ELSE 'D'
	END 
FROM DUAL;

Result would be 'B' since it is the first
correct answer
Comment

PREVIOUS NEXT
Code Example
Sql :: date sql 
Sql :: creating table in sql 
Sql :: alter or change postgresql sequence counter 
Sql :: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint 
Sql :: postgres trigger insert into another table 
Sql :: show table info mysql 
Sql :: lower case in sql 
Sql :: sqlite3 update select 
Sql :: update column value in sql 
Sql :: mysql biginteger size 
Sql :: oracle session statistics 
Sql :: Cannot truncate a table referenced in a foreign key constraint (`video_clips`.`channel_clips`, CONSTRAINT `clips_fk` FOREIGN KEY (`clip_id`) REFERENCES `video_clips`.`clips` (`id`)) in sql] 
Sql :: image for MSSQL Windows Docker 
Sql :: where condition in mongodb 
Sql :: oracle default date format 
Sql :: get triggers by query 
Sql :: Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000. 
Sql :: sql server management studio reset cache 
Sql :: check for directory in bash 
Sql :: mysql timestamp vs datetime 
Sql :: Expression number 1 of select list is not in group by clause 
Sql :: postgres parent and child tables 
Sql :: mysql command 
Sql :: sql datum formatieren 
Sql :: insert into sql 
Sql :: sql roll up rows into columns 
Sql :: sql case statement 
Sql :: foreign key on table oracle 
Sql :: soql update query 
Sql :: full outer join postgres 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =