I'm trying to do the following which involves a complex query;
1. First I try to select the column based on some logic
2. then based on the value of the selected column a value is selected
3. finally based on the previous value a logic is applied to get the final value
How can I have the result from 2 in the ELSE of the 3rd Case..Then statement ?
CASE
CASE
CASE
WHEN MDC.Network ='I' THEN INNBenefitID ELSE OONBenefitID
END
WHEN 'INNDWO' THEN CASE WHEN DWO.Description IS NOT NULL THEN DWO.Description ELSE MDC.CostShareValue END
WHEN 'OONDWO' THEN CASE WHEN DWO.Description IS NOT NULL THEN DWO.Description ELSE MDC.CostShareValue END
ELSE isnull(dbo.fn_FormatText(MDC.CostShareValue, CostShareType),'''')
END
WHEN '0%' THEN 'Covered 100%'
WHEN '$0' THEN 'Covered 100%'
ELSE ????
END
Are you looking for something like that? :
declare #value varchar(255) = (
CASE
WHEN MDC.Network ='I' THEN INNBenefitID ELSE OONBenefitID
WHEN 'INNDWO' THEN CASE WHEN DWO.Description IS NOT NULL THEN DWO.Description ELSE MDC.CostShareValue
WHEN 'OONDWO' THEN CASE WHEN DWO.Description IS NOT NULL THEN DWO.Description ELSE MDC.CostShareValue
ELSE isnull(dbo.fn_FormatText(MDC.CostShareValue, CostShareType),'''')
END
)
CASE
WHEN '0%' THEN 'Covered 100%'
WHEN '$0' THEN 'Covered 100%'
ELSE #value
END
Really, really hard to do this right without a schema or sample data. In the future consider creating a SqlFiddle for us. But one approach you might consider is using a Common Table Expression (CTE) to define an intermediary structure which you then query.
;WITH theCTE AS (SELECT
CASE
CASE
WHEN MDC.Network ='I' THEN INNBenefitID ELSE OONBenefitID
END
WHEN 'INNDWO' THEN COALESCE(DWO.Description, MDC.CostShareValue)
WHEN 'OONDWO' THEN COALESCE(DWO.Description, MDC.CostShareValue)
ELSE COALESCE(dbo.fn_FormatText(MDC.CostShareValue, CostShareType), '')
END AS foo
FROM
bar
)
SELECT
CASE
WHEN foo IN ('0%', '$0') THEN 'Covered 100%'
ELSE '' -- Do something with foo --
END AS finalColumn
FROM
theCTE
Related
I have a table content as below, where 'ItemName' is the name of the items, 'CurrentStock' is the current stock quantity of items and all other columns are the sum of stock-in and stock-out on that year. Now I want to replace all negative value with 0 and adjust those negative value with the positive value in FIFO order. Can any one guide me how do that in SQL Server.
Here is the Expected Result
Initially I tried like this
select ItemName,CurrentStock,
case when (Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013)<0 and (Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013+Qnty2012andBefore)>=0 then (Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013+Qnty2012andBefore)
when (Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013)<0 and (Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013+Qnty2012andBefore)>=0 THEN (Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013+Qnty2012andBefore)
when (Qnty2016+Qnty2015+Qnty2014+Qnty2013)<0 and (Qnty2016+Qnty2015+Qnty2014+Qnty2013+Qnty2012andBefore)>=0 THEN (Qnty2016+Qnty2015+Qnty2014+Qnty2013+Qnty2012andBefore)
when (Qnty2015+Qnty2014+Qnty2013)<0 and (Qnty2015+Qnty2014+Qnty2013+Qnty2012andBefore)>=0 THEN (Qnty2015+Qnty2014+Qnty2013+Qnty2012andBefore)
when (Qnty2014+Qnty2013)<0 and (Qnty2014+Qnty2013+Qnty2012andBefore)>=0 THEN (Qnty2014+Qnty2013+Qnty2012andBefore)
when Qnty2013 <0 and Qnty2013+Qnty2012andBefore >=0 then Qnty2013+Qnty2012andBefore
else Qnty2012andBefore end as Qnty2012andBefore,
case when Qnty2013<0 then 0
when (Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014)<0 and (Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013)>=0 then (Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013)
when (Qnty2017+Qnty2016+Qnty2015+Qnty2014)<0 and (Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013)>=0 THEN (Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013)
when (Qnty2016+Qnty2015+Qnty2014)<0 and (Qnty2016+Qnty2015+Qnty2014+Qnty2013)>=0 THEN (Qnty2016+Qnty2015+Qnty2014+Qnty2013)
when (Qnty2015+Qnty2014)<0 and (Qnty2015+Qnty2014+Qnty2013)>=0 THEN (Qnty2015+Qnty2014+Qnty2013)
when (Qnty2014)<0 and (Qnty2014+Qnty2013)>=0 THEN (Qnty2014+Qnty2013)
else Qnty2013 end as Qnty2013,
case when Qnty2014<0 then 0
when (Qnty2018+Qnty2017+Qnty2016+Qnty2015)<0 and (Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014)>=0 then (Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014)
when (Qnty2017+Qnty2016+Qnty2015)<0 and (Qnty2017+Qnty2016+Qnty2015+Qnty2014)>=0 THEN (Qnty2017+Qnty2016+Qnty2015+Qnty2014)
when (Qnty2016+Qnty2015)<0 and (Qnty2016+Qnty2015+Qnty2014)>=0 THEN (Qnty2016+Qnty2015+Qnty2014)
when (Qnty2015)<0 and (Qnty2015+Qnty2014)>=0 THEN (Qnty2015+Qnty2014)
else Qnty2014 end as Qnty2014,
case when Qnty2015<0 then 0
when (Qnty2018+Qnty2017+Qnty2016)<0 and (Qnty2018+Qnty2017+Qnty2016+Qnty2015)>=0 then (Qnty2018+Qnty2017+Qnty2016+Qnty2015)
when (Qnty2017+Qnty2016)<0 and (Qnty2017+Qnty2016+Qnty2015)>=0 THEN (Qnty2017+Qnty2016+Qnty2015)
when (Qnty2016)<0 and (Qnty2016+Qnty2015)>=0 THEN (Qnty2016+Qnty2015)
else Qnty2015 end as Qnty2015,
case when Qnty2016<0 then 0
when (Qnty2018+Qnty2017)<0 and (Qnty2018+Qnty2017+Qnty2016)>=0 then (Qnty2018+Qnty2017+Qnty2016)
when (Qnty2017)<0 and (Qnty2017+Qnty2016)>=0 THEN ( Qnty2017+Qnty2016)
else Qnty2016 end as Qnty2016,
case when Qnty2017<0 then 0
when (Qnty2018)< 0 and (Qnty2018+Qnty2017)>=0 then (Qnty2018+Qnty2017)
else Qnty2017 end as Qnty2017,
case when Qnty2018<0 then 0 else Qnty2018 end as Qnty2018 FROM StockTable
but it gave wrong output like this
Thanks
There could be two solutions for this.
Using #Temp table as shown below
SELECT ItemName,CurrentStock,
CASE when Qnty2018<0 THEN 0 ELSE Qnty2018 END as Qnty2018,
CASE when Qnty2017<0 THEN 0 ELSE Qnty2017 END as Qnty2017,
CASE when Qnty2016<0 THEN 0 ELSE Qnty2016 END as Qnty2016,
CASE when Qnty2015<0 THEN 0 ELSE Qnty2015 END as Qnty2015,
CASE when Qnty2014<0 THEN 0 ELSE Qnty2014 END as Qnty2014,
CASE when Qnty2013<0 THEN 0 ELSE Qnty2013 END as Qnty2013,
CASE when Qnty2012andBefore<0 THEN 0 ELSE Qnty2012andBefore END as Qnty2012andBefore INTO #Temp FROM StockTable
update #Temp set Qnty2018=CurrentStock WHERE Qnty2018<>0 AND Qnty2017=0 AND Qnty2016=0 AND Qnty2015=0 AND Qnty2014=0 AND Qnty2013=0 AND Qnty2012andBefore=0
update #Temp set Qnty2017=CurrentStock-Qnty2018 WHERE Qnty2017<>0 AND Qnty2016=0 AND Qnty2015=0 AND Qnty2014=0 AND Qnty2013=0 AND Qnty2012andBefore=0
update #Temp set Qnty2016=CurrentStock-Qnty2018+Qnty2017 WHERE Qnty2016<>0 AND Qnty2015=0 AND Qnty2014=0 AND Qnty2013=0 AND Qnty2012andBefore=0
update #Temp set Qnty2015=CurrentStock-Qnty2018+Qnty2017+Qnty2016 WHERE Qnty2015<>0 AND Qnty2014=0 AND Qnty2013=0 AND Qnty2012andBefore=0
update #Temp set Qnty2014=CurrentStock-Qnty2018+Qnty2017+Qnty2016+Qnty2015 WHERE Qnty2014<>0 AND Qnty2013=0 AND Qnty2012andBefore=0
update #Temp set Qnty2013=CurrentStock-Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014 WHERE Qnty2013<>0 AND Qnty2012andBefore=0
update #Temp set Qnty2012andBefore=CurrentStock-Qnty2018+Qnty2017+Qnty2016+Qnty2015+Qnty2014+Qnty2013 WHERE Qnty2012andBefore<>0
SELECT * FROM #Temp
2.Using Cursor, You could iterate on row basis and write the logic. since this is not recommended in SQL, use it only if you don't have other option.
What do you think you need the sums in the CASEs for? Given your example, it seems to me, that one CASE on each individual column has the effect you want. For the calculation of Qnty2012andBefore subtract from CurrentStock again individually applying the CASE on the summands. Like that:
SELECT ItemName,
CurrentStock,
CASE
WHEN Qnty2018 < 0
THEN 0
ELSE
Qnty2018
END Qnty2018,
...
CASE
WHEN Qnty2013 < 0
THEN 0
ELSE
Qnty2013
END Qnty2013,
CurrentStock
-
CASE
WHEN Qnty2018 < 0
THEN 0
ELSE
Qnty2018
END
...
-
CASE
WHEN Qnty2013 < 0
THEN 0
ELSE
Qnty2013
END Qnty2012andBefore
FROM StockTable;
I have two columns Speed_A and Speed_B.
Now I will compare both columns and will select the higher one in a new table.
Something like this:
SELECT ...
CASE
WHEN a.Speed_A > a.Speed_B THEN a.Speed_A
WHEN a.Speed_A < a.Speed_B THEN a.Speed_B
ELSE 0 --unknown
END as SpeedLimit,
How does it work?
Assuming your speeds are numerically typed you might get this as easy as:
SELECT CASE WHEN a.Speed_A>a.Speed_B THEN a.Speed_A ELSE a.Speed_B END AS SpeedLimit
[...FROM SomeWhere...]
Your ELSE will only occur, when A and B are equal, In this case it doesn't matter, which one you return.
UPDATE
column´s data type is integer. And yes there are nulls
If A is null return B, If b is null return A (works for A and B is null implicitly). Otherwise both are not null and therefore the statement above should be perfect
SELECT CASE WHEN a.Speed_A IS NULL THEN a.Speed_B
ELSE CASE WHEN a.Speed_B IS NULL THEN a.Speed_A
ELSE CASE WHEN a.Speed_A>a.Speed_B THEN a.Speed_A ELSE a.Speed_B
END
END
END AS SpeedLimit
FROM Dummy
[...FROM SomeWhere...]
You can create a inline max function that return the max between two values something like:
create function dbo.InlineMax(#val1 int, #val2 int)
returns int
as
begin
if #val1 > #val2
return #val1
return isnull(#val2,#val1)
end
and you can use it like this:
SELECT dbo.InlineMax(a.Speed_A, a.Speed_B)
FROM ....
Nested Case and Case without Column Name
select
CASE ColumnName
WHEN 'enter value on which you want to execute Than Block'
THEN
CASE ColumnName
WHEN 1 THEN 'return value or Column Name'
ELSE 'return value or Column Name in case of else'
END
WHEN 'another condition, value of column on which you want to execute then block'
THEN
CASE WHEN 'Condition etc => ColumnName > 5 or ColumnName ='abc' ' THEN 'return value or column name' ELSE 'return value or column name' END
ELSE 'return value or column name'
END as status
FROM TableName
I have a parameter #Destinataire in SSRS that has a list of values plus an empty string
I've created a query that I set as available values, which gives me the drop down list
SELECT code_name FROM tableA UNION ALL SELECT ''
When running the reports with the empty string, I have no results
I tried to set the parameter as text box and it does not do anything too
Yet when running the sql query I'm using to run this report, things are fine as I have all my rows retrieved (see query below)
DECLARE #DateCmt DATE = '05/09/2015',
#DateFin DATE = '05/09/2016',
#Restriction INT = 1,
#Destinataire VARCHAR(5) = ''
--
;
--SELECT #DateCmt,#DateFin
SELECT DISTINCT
CFE_EDI.IU_LIASSE
,CFE_EDI.ETAT
,CFE_EDI.DATHRMAJ -- nouveau
,CFE_EDI.ESP_APPLI
,CFE_EDI.NOM_RS
,PARTENAIRES.LIBEL
,PARTENAIRES.CODE_INSEE
,CFE_EDI.DATHR_ENV -- nouveau
,CFE_EDI.DATHR_MEF -- nouveau
,CFE_EDI.DATHR_PRE -- nouveau
,CFE_EDI.DATHR_OUV -- nouveau
--,CFE_EDI.DATEHR_DEPOT-- mettre l'heure
,CFE_EDI.GESTDEL
--,CFE_SERVICE_DEST.IU_DEST
--,CFE_SERVICE.IU_LIASSE
,CASE WHEN CFE_EDI.ETAT = 'MEF' THEN 'En Attente le'
WHEN CFE_EDI.ETAT = 'PRE' THEN 'Préparé le'
WHEN CFE_EDI.ETAT = 'ENV' THEN 'Envoyé le'
WHEN CFE_EDI.ETAT = 'OUV' THEN 'Réceptionné le'
WHEN CFE_EDI.ETAT = 'NRM' THEN 'Non remis le'
WHEN CFE_EDI.ETAT = 'NAQ' THEN 'Non acquitté le'
END AS ChampEtat
,CASE WHEN CFE_EDI.ETAT = 'OUV' THEN 'Date d''envoi : ' + CONVERT(VARCHAR,CFE_EDI.DATHR_ENV,103)
END AS Date_Envoi,
CASE
WHEN CFE_EDI.ETAT='MEF' THEN CONVERT(VARCHAR,CFE_EDI.DATHR_MEF,103)
WHEN CFE_EDI.ETAT='PRE' THEN CONVERT(VARCHAR,CFE_EDI.DATHR_PRE,103)
WHEN CFE_EDI.ETAT='ENV' THEN CONVERT(VARCHAR,CFE_EDI.DATHR_ENV,103)
WHEN CFE_EDI.ETAT='OUV' THEN CONVERT(VARCHAR,CFE_EDI.DATHR_OUV,103)
ELSE CONVERT(VARCHAR,CFE_EDI.DATHR_DEPOT,103) END AS DateMaj ,
CASE
WHEN CFE_EDI.ETAT='MEF' then CONVERT(VARCHAR,CFE_EDI.DATHR_MEF,108)
WHEN CFE_EDI.ETAT='PRE' then CONVERT(VARCHAR,CFE_EDI.DATHR_PRE,108)
WHEN CFE_EDI.ETAT='ENV' then CONVERT(VARCHAR,CFE_EDI.DATHR_ENV,108)
WHEN CFE_EDI.ETAT='OUV' then CONVERT(VARCHAR,CFE_EDI.DATHR_OUV,108)
ELSE CONVERT(VARCHAR,CFE_EDI.DATHR_DEPOT,108) END AS HeureMaj,
PARTENAIRES.LIBEL + '(' + CFE_EDI.CODE_INSEE + ')' AS LibelDestinataire
--,CASE WHEN #Restriction = 1 THEN '1'
-- WHEN #Restriction = 0 THEN '0' END AS Restriction
,CASE WHEN #DateCmt != #DateFin AND #DateCmt < #DateFin THEN 'Diffusion Xml du ' + CONVERT(VARCHAR,(#DateCmt),103) + ' au ' + CONVERT(VARCHAR,(#DateFin),103) ELSE
'Diffusion EDI Xml du ' + CONVERT(VARCHAR,#DateCmt,103) END AS Plage_Diffusion
-- INTO
FROM
(PARTENAIRES
INNER JOIN dbo.CFE_EDI ON PARTENAIRES.CODE_INSEE = CFE_EDI.CODE_INSEE)
INNER JOIN dbo.CFE_SERVICE ON CFE_EDI.IU_LIASSE = CFE_SERVICE.IU_LIASSE
INNER JOIN dbo.CFE_SERVICE_DEST ON (PARTENAIRES.IU_PART = CFE_SERVICE_DEST.IU_PART_CFE)
WHERE
case when #Restriction = 1
then case when CFE_EDI.ETAT in('ENV','OUV') then 1 else 0 end
when #Restriction = 0
then case when CFE_EDI.ETAT not in('ENV','OUV') then 1 else 0 end
else case when CFE_EDI.ETAT <> '' then 1 else 0 end
end = 1
AND
CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE != ''
AND CASE --WHEN CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE !=''
--THEN CASE
WHEN #Destinataire != '' AND (#Destinataire) IS NOT NULL
THEN CASE WHEN CFE_EDI.CODE_INSEE = #Destinataire THEN 1 ELSE 0 END
ELSE CASE WHEN CFE_EDI.CODE_INSEE = PARTENAIRES.CODE_INSEE
AND cfe_edi.dathrmaj > #DateCmt AND cfe_edi.dathrmaj < #DateFin
AND CFE_EDI.GESTDEL = '1' THEN 1 ELSE 0 END
END = 1
First question would be to know if there is way to setup the parameter without using my stupid trick.
Second question is why the query with parameter with an empty string does the trick and once you use SSRS, nothing.
Thanks in advance for your help
Update I tried to set the WHEN LEN(#Destinataire) > 0 with the #Destinataire = '' but no luck on that one.
Update 2 My aim now is to have a solution that will retrieve all the datas, in case the #Destinataire is equal to '' or NULL. However, thinking about it, this solution is equivalent to having all the values populated in #Destinataire. So one way or another, I would say.
Final update I've recreated everything from scratch and oh! magic, the grouping or the everything option worked as wish. I still don't know what was wrong but I'm fine with the results. Many thanks for your help and support.
looking at your query and where you pass #Destinataire you should be able to pass a null value according to the where clause and get the same effect as passing ''
CASE --WHEN CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE !=''
--THEN CASE
WHEN #Destinataire != '' AND (#Destinataire) IS NOT NULL
THEN CASE WHEN CFE_EDI.CODE_INSEE = #Destinataire THEN 1 ELSE 0 END
ELSE CASE WHEN CFE_EDI.CODE_INSEE = PARTENAIRES.CODE_INSEE
AND cfe_edi.dathrmaj > #DateCmt AND cfe_edi.dathrmaj < #DateFin
AND CFE_EDI.GESTDEL = '1' THEN 1 ELSE 0 END
END = 1
I would try to just set that particular paramter to allow nulls in the ssrs report which can be found as a checkbox in your parameter settings window.
There is a hybrid option, where you can keep your visible parameter just as you have it, but then use a hidden cascaded parameter to reference in the query. So if you really are having trouble with formulating your query with blank or null values as a parameter, this will work around that. Here are the steps:
Create a new dataset.
A slight catch, you do need to make sure that the changing the selection always adds a new value to force a refresh of the cascaded parameter, but that's easy to do:
SELECT code_name FROM tableA WHERE #Destinataire='' OR #Destinataire=code_name
--Dummy value needed to force update of parameter value in some cases:
UNION SELECT 'zz' + #Destinataire AS code_name
Set up a new parameter, #MultiDestataire. Allow it to accept multiple values and use the new dataset as its available and default values. Set its visibility to Hidden.
Running the report before hiding the new parameter shows how it works:
Edit the query to use the new multivalue parameter in its WHERE clause.
From here on, this is just altering the query to use the IN statement, which should look like this:
CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE != ''
AND CFE_EDI.CODE_INSEE IN (#MultiDestinataire)
...
Attempting this may end up being an intermediary step if you've got some other issue in the query that's responsible for unexpected results, as you should be able to test for an empty value there.
This is just my two cents, basically you have two options here.
Multivalue parameter Option
Define your parameter to allow multivalues, use the same dataset in Available values and Default values properties. So when your user doesn't select any value the parameter will be populated with all values. Otherwise your parameter will be populated only with values your users select.
In this case you will have to use IN operator since your parameter represent multiple values seleted or not by your user.
...
WHERE CFE_EDI.CODE_INSEE IN (#Destinataire)
...
When you use Default values property all drop down list values are selected by default and your report run without any filter being applied (at least if your user doesn't select any value or values).
Additional, I'd avoid use '' (blank), it is uninformative and your user might think it is some kind of error or your parameter was not populated properly.
Why use a single value parameters when you want to show data from more than one value (in your case all)?
Single value Option (No sense IMO)
In order to this works you have to set your parameter to Allow blank value ("") and Allow null value. Your query should look like this:
WHERE
CASE WHEN #Destinataire = '' OR #Destinataire is null THEN 1 ELSE 0 END = 1
OR
CFE_EDI.CODE_INSEE = #Destinataire
In your query I think it could be something like this:
WHERE CASE
WHEN #Restriction = 1 THEN
CASE
WHEN cfe_edi.etat IN( 'ENV', 'OUV' ) THEN 1
ELSE 0
END
WHEN #Restriction = 0 THEN
CASE
WHEN cfe_edi.etat NOT IN( 'ENV', 'OUV' ) THEN 1
ELSE 0
END
ELSE
CASE
WHEN cfe_edi.etat <> '' THEN 1
ELSE 0
END
END = 1
AND cfe_edi.code_insee IS NOT NULL
AND cfe_edi.code_insee != ''
AND ( CASE
--WHEN CFE_EDI.CODE_INSEE IS NOT NULL AND CFE_EDI.CODE_INSEE !=''
--THEN CASE
WHEN ( #Destinataire = ''
OR #Destinataire IS NULL )
AND cfe_edi.code_insee = partenaires.code_insee
AND cfe_edi.dathrmaj > #DateCmt
AND cfe_edi.dathrmaj < #DateFin
AND cfe_edi.gestdel = '1' THEN 1
ELSE 0
END = 1
OR cfe_edi.code_insee = #Destinataire )
Also consider this evaluation AND cfe_edi.code_insee = partenaires.code_insee, is it necessary even if your JOIN operator is forcing to meet the condition? INNER JOIN dbo.CFE_EDI ON PARTENAIRES.CODE_INSEE = CFE_EDI.CODE_INSEE
A third option could be use a hidden parameter to clean the null and '' option of the user to produce a parameter populated with all values. Try the options above before get involved with hidden/internal parameter approach.
Let me know if this helps.
Case stud.Status
when 'N'then 'NA'
end
How to add following condition before when statement above
IF #ID is not equal to 2. That is I want to assign 'NA' only when also the id <> 2 plus the above condition.
EDIT: Full Query
Case Stud.Status
when #ID = 2 AND Stud.Status = 'N' then 'To Be Submitted'
when 'N'then 'N/A'
else isnull(Stud.Status, '')
end
Like this:
CASE
WHEN #ID <> 2 AND stud.Status ='N' THEN 'NA'
END
This is the other form of the CASE expression.
Note that: The CASE expression has two forms:
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
The searched CASE expression evaluates a set of Boolean expressions to determine the result.
Update: Try this instead:
'StudStatus' =
CASE
WHEN #ID = 5 AND Stud.Status = 'N' THEN 'To Be Submitted'
WHEN Stud.Status = 'N' THEN 'N/A'
ELSE ISNULL(Stud.Status, '')
END
try this
Case stud.Status
when 'N' and #id<>2 then 'NA'
end
if #ID <> 2
begin
Case stud.Status
when 'N'then 'NA'
end
end
How would I achieve the following:
select (1 < 2) as One, (1 > 2) as Two
so that it would yield the following results:
One Two
-----------------
True False
I'm using SQL Server but a cross DBMS example would be good.
Assuming this is SQL server, you can use CASE statement.
select (case when (1 < 2) then 'True' else 'False' end) as one,
(case when (1 > 2) then 'True' else 'False' end) as two
from table
In the place of condition, you can use any variable or any column values too. Basically an expression.
Well, in Oracle you could do something like
SELECT CASE
WHEN 1 < 2 THEN
'TRUE'
ELSE
'FALSE'
END AS ONE,
CASE
WHEN 1 > 2 THEN
'TRUE'
ELSE
'FALSE'
END AS TWO
FROM DUAL;
Note that Oracle doesn't have a BOOLEAN type in the database (as opposed to PL/SQL, which does have BOOLEAN's) so the case expressions return character strings.
Share and enjoy.
Use a case statement:
declare #value1 int, #value2 int
set #value1 = 1
set #value2 = 2
select
case when (#value1 < #value2) then 'True' else 'False' end as One
case when (#value1 > #value2) then 'False' else 'True' end as Two
from table
Depending on your need, you can hard code in the values, or you can do something similar to this for when you may want to change the values. You could also combine the case statement into one column, or break it out for doing less than or equal type comparisons as well.