Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sql select case when

--CREATE SOME SAMPLE DATA

CREATE TABLE Casedata
  (
     NAME VARCHAR(10)
  );

INSERT INTO Casedata VALUES ('Test 1');
INSERT INTO Casedata VALUES ('Test 2');
INSERT INTO Casedata VALUES ('None');

-- CASE WHEN IN SQL
SELECT NAME,
       CASE
         WHEN NAME = 'Test 1' THEN 'The value is 1'
         WHEN NAME = 'Test 2' THEN 'The value is 2'
         ELSE 'Sorry no Match'
       END
FROM   Casedata 
Comment

case statement in select query in sql

INSERT INTO HumanResources.departmentcopy(DepartmentID, GroupName, Name, temp)
SELECT  DepartmentID,
        GroupName,
        Name,
        CASE WHEN DepartmentID = 1 AND Name = 'Engineering and Research'
        THEN 'sucessful' ELSE 'unsucessful' END
FROM HumanResources.department
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 :: psql shell 
Sql :: set column width in sqlplus 
Sql :: mysql default port number 
Sql :: mysql mediumtext 
Sql :: cast in sql 
Sql :: Mysql table variables 
Sql :: java sql insert return id 
Sql :: mysql size of database 
Sql :: Split JSON data in SQL Server column 
Sql :: sql unique 
Sql :: having in sql server 
Sql :: sql 2 way of select unique 
Sql :: sql count() 
Sql :: enum in sql server 
Sql :: mysql error 1114 (hy000) the table is full 
Sql :: sqlite clear shell 
Sql :: oracle step procedure 
Sql :: sql trying to delete database in use 
Sql :: check if table is Temporal 
Sql :: hour must be between 1 and 12 
Sql :: windows aggregate functions in postgresql 
Sql :: sql server size of every table in a db 
Sql :: mysql show column type 
Sql :: triggers in mysql example 
Sql :: sql order by except one row 
Sql :: longtext sql 
Sql :: how convert into in in sql query 
Sql :: what is between keyword used for 
Sql :: postgres advance auto increment 
Sql :: sql full outer join 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =