case in sql

Solutions on MaxInterview for case in sql by the best coders in the world

showing results for - "case in sql"
María Fernanda
03 Feb 2018
1-- NOTE: this is for SQL-Oracle specifically
2
3/*
4NB: Please like Mingles444 post, I derived this from him/her
5*/
6
7-- syntax: (Retrieved from grepper:Mingles444)
8CASE
9    WHEN condition1 THEN result1
10    WHEN condition2 THEN result2
11    WHEN conditionN THEN resultN
12    ELSE result
13END 
14
15-- example:
16SELECT 
17	CASE
18      WHEN (1+6 = 6) THEN 'A'
19      WHEN (1+6 = 7) THEN 'B'
20      WHEN (1+6 = 8) THEN 'C'
21      ELSE 'D'
22	END 
23FROM DUAL;
24
25-- OUTPUT: B
26
Gabriel
15 Feb 2018
1-- Case Eg.) to retrive the MAX value of a Field 
2-- if there are entries for the Field in table MAX value will be returned 
3-- But if there is no entries at all for the Field in tabel MAX will return
4-- Null as the output. But Using Case When we can check it out return zero 
5-- or any other value if there is no enties for the Field in table..
6SELECT 
7CASE   -- Like Switch Case
8	WHEN -- First When condition 
9		(MAX(BILLID) IS NULL) -- Condition 
10	THEN 1   -- output   (We can also add more When conditions like Above)
11ELSE -- When WHEN Condition not Satisfied Below will be Executed. 
12		(MAX(BILLID)) -- output
13END 
14as MAXBILLID   from  DUAL;
15-- Final Output
16-- If there is no entry in the Field for the table
17-- BILLID
18--  1
19-- If there are entries MAX of that Field value from the table
20-- BILLID
21-- 10
Charlie
29 Jan 2021
1/*CASE statements are used to create different outputs and is 
2  used by SQL as a way to handle if-then logic.*/
3  
4  SELECT column_name,
5    CASE 
6      WHEN condition THEN 'Result_1'
7      WHEN condition THEN 'Result_2'
8      ELSE 'Result_3'
9    END
10  FROM table_name;
Liesl
22 Apr 2017
1select 
2case when ID in ('1', '2', '3')
3then 'Jack'
4else 'Jim'
5end as Person
6from Table.Names
7
8select
9case when ID in ('1', '2', '3')
10then 'Jack'
11else 'Jim'
12end Person
13from Table.Names
Judith
26 Jan 2016
1Change query output depending on conditions.
2Example: Returns users and their subscriptions, along with a new column
3called activity_levels that makes a judgement based on the number of
4subscriptions.
5SELECT first_name, surname, subscriptions
6CASE WHEN subscriptions > 10 THEN 'Very active'
7WHEN Quantity BETWEEN 3 AND 10 THEN 'Active'
8ELSE 'Inactive'
9END AS activity_levels
10FROM users;
Isabel
21 Feb 2020
1
2  SELECT OrderID, Quantity,
3CASE
4    WHEN Quantity > 30 
5  THEN 'The quantity is greater than 30'
6    WHEN Quantity = 30 THEN 'The 
7  quantity is 30'
8    ELSE 'The quantity is under 30'
9END AS QuantityText
10FROM OrderDetails; 
queries leading to this page
case sql statementsql server case when in select statementsql end statementcase when conditioncan i use or in a case statement sqlsql server case when and conditioncase when conditions sqlsql case themcase statement in select statement sqlsql case statement with ordoes case work in sql servercase when querycase statement inside select query sql servercase inside case sqlcase when with in clause sqlcase and sqlsentence case in sqlcase when staementcase whn then in sqlsql swith caseselect case and sqlcase sqlcase sql queryusing case in statement queryt sql case statement examplesql case when and orsql case when incase when in ms sqlsql syntax casecase statements in sql are used in what clause 28s 29 3fcase statements in sql servercase queryhow to use case in a sql querysql server case when inhow to apply case in sqlsql server add case to wherecase when in in sql serverwhere then clause sqlcase query in sqlsql condition on selectsql select from caseuse case statement in sqlsql case functionsql case switchsyntax sql case whenend sqlcase table sqlsql or in case statementsql syntax case whenquery select casecase sql statement is which of the followingsql case variablewhat is end as in sqlcase when in sql incase in case sqlcase in select statement sql serversql query usinf case statementcan case be use before select statementsql case end ascase 28 29 in sqlif else in select statement sqlsql simple case statcase when examples sql serversql when else casesql cas estatement toselect case in sql serversql where switchor condition in sql case statementcase statement end withcas ein sqlsql conditional statementcse statement with where close ms sqlconditional statements in sqlcase with wher in sqlcase when and 2b sqlconditional statements insqlselect 2a case sqlcase when select statement sqlsql case wheresql case statementif case inn sql queryif else in sql server selectsql case expressionsql proper casecase when sql and orplsql casewhen case with select condition in sqlsql casehow to write case statment in sqlswitch case querycase when use in sql server select with case sqlselect sql casewhat is a case statementcase statement where sql 3d in case sqlwhen in sqlcase statement 40query in sqlsql server caswe whencas when selectsql case whencase when in sqlselect query casesql where switch casecase is a clause of statement in sqlwhat is select case statement sqlsql case when functioncase expressions in sqlsql whenrun select on case when sqlusing select statement in case when sql serversql case when ascase when example in sqlsql select ifcase statement in sql add numbershow does case work in sqlcase query examplequerying data using the sql case statement case selectsql case statement in selectsql case statmentcase statement in a querycas when in sqlselect in case statement sqlselect statement in case statementswitch case in sql serverswitch case sql exampleselect if from sqlselect in case statement in sql serverselect case when in sqlcase condition sqlcase statement in select statement case keyword in sqlcase switch sqlsql case thent sql statement endselect by case sql servercase or sqlselect else nullcase when structurecase as sqlsql condition statementsql case statement sql query case whenswitch case syntax in sql serversql where case statementcase tsqlselect case when sql case in wherecase in sql expressionconditions in select statement sqlcase statement sql examplein cause with case statement in sqlend as sqlsql case ehen nullhow to use case when in sqlselect as sql casesql select where case statementselect query case statement sqlcase when else sql serverselect case when sqlhow to use case statement in sqlwrite case in sqlcase when sqmultiple case when sqlwhen case in sql serversql case when elsecase in a case sqlcase sql syntaxselect case ifwhen do you use a switch case in sqlif case in sqlcase when 2b in sqlsql switch functioncase when sql nullhow to write case in sqlcase when sql elsesql case with andksql whe swith casesql in casecase in select sqlcase in sqlcase statementusing when n sql1 3d case in sqlsql where conditionsql case when otherwisesql or statement in case statementcase sq 3busing case in sqlin case statement sqlcase end sqlcase when sql 3bsql cse statmentif statement in sqlcase command in sqlcase functions sqlsql set casecase when structure sqlsql case then ascase when or sqlhow can a case be used in sqlhow to set case condition in sqlsql server case when nullsql case else keep sameselect if value then text sqlsql case wensql format case statementselect caseif and then sqlotherwise sqlcase expression in sql servercase statement condition select query sqlsql case and orsql case studieswhat does case when does in sqltsql select case whencase statements sql owhen sqltsql case statementsql case asadd case in select statement sqlhow to write swtch case in sql serversql using select caseuntill change the login time in sql how to write casewhen case in sqlcase if value sqluse case sqlhwot o switch from on clause sqlslq case usagesql case stamentsql when then statmenet as case whencase with selectwhen we use case statement in sqlcase when else end sqlend sql querycase when por coluna sqlcase when select sqlselect case when thenuse of case when in sqlcase expression in sqlsql how to end in a function if value is not metfrom case sqlwhy use case in sqlimplementation of case when function in sqlselect on case when elseselect switch sqlcase statementssql select caseif and case statement in sqlcase then sql andwhen case syntax sqlsql case when orand in case statementscase i sqlcase whenqlproper case sqlcase statemetn in sqlcase else sql andwhere case in sqlswitch case statemnt sqlcase statements in sql are used in what clause 28s 29sql case when null then select elseselect if statement sqlcheck a no can be this or this in case sqlslq server casesql function switch caseend as in sqlcase with and condition in sqlsql case statementsdselect query based on case sqlselect conditional sqlcase in sql qyery case sql defaulthow do you do if then statements in sqlsql all queries casewhen value then select sqlcase when sqlcase when in squl 27sql condition in select statementsqlserver case statementcase statemnts sqlquery casehow to use case condition in sql querycase when with as in sqladd as case when sqlsql query with casesql conditional 3c 3ewhen and then in sqlswitch case in sqlsql switch fromcase 2c microsoft sqlend in sqlwhen statement in sql serverans sql case whensql case statement exampleif then in sqlcase in select columns ssmasql when thenswitch cases in sqlwhat is case statement in sql 3fcase in sql sql serverwhat is a case in sqluse case in where sql serversql if statement in select queryselect case query in sqlcase operator in sqlsql case defaultcase when thensql select switch casecase by sqlselect and case when sqlexamples for case in sqlcase example in sqlwhen case in where clause sqlset case in sqlt sql casesql conditional statementscase in sql where clausecase when else sqlcase statement in sql functionaccess case when sqlwriet query in then incasesql server select from table case statementt sql case whenif case querycase when sql selectt sql if statement in select caseselect case true when sqlcase sql accesssql query with ifdiferent between two ways to write case statement in sqlmssql where case when thensql thenhow to type case in sqlt sql is null in caseseelct casecase statement includes when in 28 29 sqlsql server whencase then as sqlms sql case 09 09whensql where thenwhat does case do in sqluse case in sql selectcase statements in select statement sql servercase in sql serverand clause in case statement sqlmssql case with andcase stament in sql serverdo stmt in a stmt sqlwhen then case sqlcase for in clause sqlcase syntax in sqlhow to code use case in sqlcase statement 3bcondition in case sqlgeneral case syntax in sqlelse when sqlswitch sql serverswitch case sql columncase then sqlcase statement else ifas case when sqlcase in sql selectcase statement in insert queryms sql seldt case withselect in case statementselect query in case when sqlsql case when nullsql case when then elseif and else condition in sqlor in case statement sqlmssql select casecase with select statement in sqlselect case sql serverhow do you write a case condition in sql query 3fend with a in sqlsql case in select statementcase condiittion sqla case sql statement iscase when elsewcaseoracle sqlcase when sql t sqlcase when switch in sqlsql if case 3d casecase set sqlcase when syntax sqlswith case i sqlsql where and casehow does sql case statement workswitch case value between sqlset case sqlhow to use case statement in ms sqlwith as casesql select case wheresql when and thencase when and else case in sqlhow to use case in sqlif statement in case statementhow to use sql case statementcase statement in where sqlwhen then sqlcase statements sqlcase statement sql servercase when in selectsql string proper casecase in mssqlcase on query in sqlcase statement in case statement sqlcase whesql whriting a case conditionall possible case statements in sqlswitch case sql servercase when t sqlsql case statement syntaxcasein sqltype case in sqlcase sql examplein teh case statement then clause with a sqlcase when syntax in sqlconditional statement in sql servert sql caseadd as case when then else sqlcase pl sqlsql server case in wheresql when case thensql case statement in where clausewhat in case when sqlsql case statements deciding querycase when msqlcase sql server with select e2 80 a2 how do you write switch statement in sql 3fsql if case where clausecase statement in sql 3e 3cif then else with 2 condition in sql statementcase with or condition in sql servercase return sqlor condition in case statement in sqlcase when sql andquery sql conditionalcase condition to check value in sqlcase in where sqlswitch case with in case 27 in sqlservercase when sql examples and solutionscase when statements in sql orsql select case nullsql 2bcase when 2bdefiationcase qhen sqlwhenn sqlcase when whensql server case 22 2b 22 casestudy case select from select statement sqlsql server case when and case where sqlsql then after a case statementcase statement sq 3bmssql case when thencase in ms sqlwhat is case when in sqlcase manipulation functions in sqlselect using casse statement in sqlcasesqlmssql case whencase when statement sqlcase statement string statement t sqlsql select case endcase when condition selectswitch statement in sqlcase sql in where clausesql case truecondition in select statement sqlsql case when statementssql select casetsql case when nullsql if thenselect with case in sqlorder by and switch case sqlsql use a case in a casecase when in case when sqlsql with casesql conditionalcase when else ifcase sql statmentwhere case when sqlcase wqhen sqlcase statements in sql are used in form only 3fcase sql examplescase statements in sqlcase en sqlupdate with case statement in sqlsql case whencase when and ifconditional query in sqlcase where sql sqlselect query within sql casesql case statement exampleswhich statements can case statement be used for in sqlcase sql server examplehow case when works in sqlwhen condition in select querycase statement use case statement sqlsql case when then if elseifcase statement example in sqlwhat is use of case in sqlsql server respect casedeclare with case when sqlwhere and case sqlsql case elsecase when in sql with examplecase when sql serverswitch case and for loop in sqlcase sqlswitch case with aggregat in sqlcase statement sqsql wann casesql case sqlcase statement in sql server examplesql switch tosytax case whenswicth case t sqlsql switch statementselect using case statement in sqlcase statement with sqlwhen than in tsqlcase when use in sqlsql server when case selectcase when sql srevrsql case and statementhow to do a case in sqlcase null in sqldisplay all the cases of case statement togetherend with sqlsql function using switch casewhat is end in sqlsimple case expression sql 3c in case statement sqlcase wehn else sqlsql where with case statementuse of 27a 27 in case sql case on sqlthe use case sqlswit case sqlcase when statement in sqlcase statement in sql select queryt sql case statementaccess sql casesql case examplescase with where in sqlcase as condition sqlassign value using case statement in sqlsql case sywhere with case sqlwith case in sqlsql server switchsql case when in wheresql server case function syntaxcondition in select sqlsql switch syntaxswitch statement sqlcase when null sqlsql case henif condition use with case when then in sql serverand in when condition in sqluse case with in operator in sqlcase sql serercase in when case sqlsql case statement whereselect in case statement in sqlhow to use or condition in sql casehow to put case in where clause in sqlwhen statement in sqlsql case and whenhow to write if else statement in sqlcase end when null sqlcases in sqlselect case in sqlcase when as column sqluse of case in sqlcase with in operator in sqlsql select case when nullsql where case syntaxcase when null then sql serverif a case of exampleuse function in case statement sqlcase statement sql to set valuewhen then case in sqlcase swicth in sqlsql select case aswhen else sqlsql where casecase when queryswhat is sql case statementnull in sql case statementsql case 5cthenwhen and else sqlcase mssqltsql select casesql server casesql select conditionalt sql select case sql case examplecase when sql fromsql if else casessql case query in where clauseif case is elsecase when in select statementsql case select case whenselect case from sqlhow to use select statement in case statement in sqlquery in case statement in sqlusing case sqlcase when in sql in where clauset sql case when sql server case when examplesql case when if else ifusing sql case statementhow to use case function in sqlcase when examplecase when then in sql servercase then in sqlhow to use case in update statement in sqlcase statment or case sqlsql use casecase when elsesql case when null then select switch case sql selectcase clause in sqlsome condition case wehn in sqlcase function in sql examplemssql select case examplewhich of the following is a case sql statementsql case and conditioncase statment sqlt sql case when elsecase if else when in mssqlcase syntaxcase and when statement in sqlcase in sql server where conditional statements sqlusing case when in sqlsql case whwnsql case when thensql whre in casesql when statementcase in select statement sqlswitch function in sqlmssql case in selectt sql select casecase select in sqlcase when with and condition in sqlswl case whenhow to user case in sqlcase in querysql select case andcan i use in in a case statement in sql serversql when casetsql if else in casecase in tsqlsql conditional 3c case when statements in sqlsql if theencase statement t sqlsql select data case when 2bcasw sqlcase switch in where clause sqlcase for an ifcase clause sqlswitch in sqlcase if sqlcase when query in sqlsql end asi fthen sqlor in case when sqlif in case statement sqlwhat case does sql useswitch case in sql selectt sql case statement in select clausesql case keywordsql server management studio when casecase statement in sql querysql server case with a messagewhen we use case statement in sql with examplecase for select a querysql switchcase sql serveruse in in case statement sqlcase when statements sqlsql server switch casecase sentence sqlcase when and sqlcase use in sqlupdate sql with casecase statement exampleshow to implement when case in sqluse case with where in sqlcase when in sql serversql case statement with characterssql case statementshow to case statemetn elsecase in sql examplesql case in selectsql select case in as clausecase in sql queryusing case in sql servercase statement in a where clause in sqlselect case mssqlwhen and case in sqlproper case in sqluse of when in sqlif then statement in sqlcase in where statement sqlcase else if sql serverwhen satement in sqlwhen case in sql sql select switchcase statements in sql are used in what with where 28s 29sql case when exampllesql the case statementsql endmicrosoft sql server case statementsql query select casecase from sqlcase when then selse sqlwhen and else condition in sqlwhere and case in sqlsql case selectcase sql server statementsql when statement 5csql select with conditionsql server case when select queryswith case in sqlcase statement in sql 2c case sameswitch case sql querycase when then in sqlsql server select case whenif use with case when then in sql serverhow to use and in case statement in sqlcase in where clause sqlhow to write case when with select condition in sqlusing or in case statement sqlthe sql case statementsql cas in selectsql if casecase with when sqlsql caecase when distinto sqlcase state ment in sqlcase conditiont sql case statementis sql case 23case select when sqlsql else wherecase statement in sql servercase 3e 0 sqlcase when in mssqlsql case with andow would a case sql statement be usedwhat is case in sqlcase when sql querymssql caseselect case where nullsql switch caseexamples of select query where condition in case statement in sql serverwhat is case statement in sqlsql if case where nullhow to use sql casesql case statement in fromcase in from clause sqlsql casesor statement in sql server casewhere with case statement in sql serversql case clausehow would a case sql statement be usedsql server 2016 case statementwhen statement sqlselect case statement sqlsql query for case whenselect whenwhen case sqlhow to write a case statement in sqlsql case in where clausesql use functionin caseuse cases in sqlsql case when in case whenapply a parameter in case statement in sql servercase in select statementcase when then else sqlsql case statement whenselect statmet 2b case 2b sqlcase in access sqlwhy we use case in sqlsql select case statementcase in sql server querytsql select where casecasel in sql server examplehow do you write switch in sqlcase statement in when then insqlcase in a select statement sql serverhow does case statement work in sqlcase when in sql selectsql query when caseswitch case statement sql servert sql case when examplesql switch case in selectwhen then in sqlcase statmentssql witing boolean or in casehow to learn sql case whenquery case sqlsql query where use case whenusing in in case statement sqlcase when to give a set of numbers in sqlsql server case statementhow to use case with in operator in sqlexample of case in sqlsql can when thenwhere with case in sqlcase syntax sqlcase when sqksql server case whenselect case then sqlcase when stuff sqlcase when then select sqland case sqltsql caseswitch sqlsql server switch case defaultsql adding 2 case statementsswitch case and count in sqlsql case constructtsql case example sql case insql case andcase when en sqlwhen condition in sqlend 2a sqlsql server select casecase number sqlhow to make case with if statementcase sql selectcase statement sql server or operatorcase tutorial sqlcase when ms sqlcase in select query when statement in sqlcase statement with where sqlselect case create database sql servercase when selectcase else ifsqlserver casecase in sql esql case operatorcase function sql examplesql case orcase condition in sqlcase thensql server case when elsecase example sqlcase statements in select sql serverend as sql meaningsql create case whensql server case statement end ascase statements in sql are used in what clause 28s 29 3f 2acase when sql is nullexample of case when in sqlselect from case when sqlhow to apply condition in case using query in sqlsql case is nullksql using or in case statementsql case ifcase when doselect case inswitch case in sqlserverwhen casehow to apply case to 3e 3d condition in where sql serversql case dentor de casewhere then sqlcase statement when null sqlcase when then sqlwhen condition sqluse case in sqlsql if statement in selectcase statmentswitch case with query sqlselect case statementcasel in where sql server exampleuse case for sqlcase in statement in sqlcase when else end sql serverwhy is case statement used in sqlmssql case when elseselect query with case statement in ssmsselect case sqlservercase condition in sql serversql query case statementcase with or condition sqland in case sqlwhere and case statement sqlcan i add right function using case statement sqlsql condition statementsselect with casecase stetement in in sql serverswitch case sqlt sql case ifsql server when casesql when case examplehow to write in sql logic case when until change the time case null sqlhow to do a case statement in ms sqlcase select t sqlsql case when is nullms sql casecase end aswhere case sqlcase wheensql end as 3fsql based on casecase sql server selectcan you use a case statement in a create statement sqltsql case whenmssql case statementcase of a select statement sql serverselect switch case example on sqlusing case in sql statementsuse case in and sqlsql case stringcase when syntax in sql servercase statement sqlswitc case in sqlsql select case whensql end querycase statement in sqlcase function in sqlsql case statemenlogical or in sql case statementwhen case sql serversql use a case and on the wheremssql whencase in sql statementwhat case do in sqlt sql case examplenull casecase else sqlsql case when andcase functions in sqlcase when case when sqlsql query end statementsyntax for when case in sqlhow to write case in where condition in sqlsql field casecase when psqlreward service reward usages and payment service orders sql case studycase in select statement in sqlsql case tsql case is nullif else sql selectcase when sql exampleswicth case sqlsql case when examplesql csecase on case sqlhow to write case statement in sqlselect case sqlcase whenwhat is case expression in sqlsql switch stringusing case statements in sqlcase in sql with and conditionsql switch case statementcase else in sqlcase in sql tutorialsql query in case statementcase sq1leritong conditoon in sqlt sql case when elsesql case select statementsql case syntaxhow to case with in sql querycase in sql