Using a CASE WHEN with multiple Whens that is only resulting in a BOOLEAN value - sql

I am working on writing a SQL Query which is using a CASE line with multiple When's, and I was wondering why my result is only showing BOOLEAN (True/False) Values. Ideally I would like it to show whatever is after the THEN's in my code.
--with loan as (
SELECT DISTINCT
rtw.identifierdimsk
,lmmf.milestonesk
,lmd.groupname
,rtw.typecode
,rtw.campaignname
,rtw.partnername
,rtw.sourcename
,lmmf.date
,rtw.typecode = CASE WHEN rtw.typecode = 'PROS' THEN 'Prospect'
WHEN rtw.typecode = 'CARI' THEN 'Deluxe'
WHEN rtw.typecode = ('CARIC') OR rtw.typecode = ('CTE') THEN 'CSignal'
WHEN rtw.typecode = 'RMT' THEN 'Pre Pull'
WHEN rtw.typecode in ('90','90PLUS','C90','C90PLUS','BP','ELO','AUTO','POP') THEN 'In-process'
WHEN rtw.typecode = 'XU3%' THEN 'U3'
WHEN rtw.typecode = '%CT%' THEN 'Misc'
END AS Value
FROM rtal_winner rtw
INNER JOIN one_fact lmmf ON lmmf.identifierdimsk = rtw.identifierdimsk
INNER JOIN one_dim lmd ON lmd.milestonesk = lmmf.milestonesk
AND lmmf.date >= '2022-10-25'

Because you are comparing rtw.typecode to the result of the case expression:
rtw.typecode = CASE ...
remove the comparison part:
, lmmf.date
, CASE
WHEN rtw.typecode = 'PROS' THEN 'Prospect'
-- ...
END AS Value
FROM rtal_winner rtw

Related

case when using multiple conditions across multiple columns

So quick example:
I need to change that 'imperial' to 'Metric' in the UnitsofMeterRemoved Column. However it has to be when meter model = g4 and MeterManufacturerRemoved = KROMSCHRODER.
I have tried this
SELECT *,
CASE WHEN MeterModelRemoved = 'G4'
AND MeterManufacturerRemoved = 'KROMSCHRODER'
AND UnitsofMeterRemoved = 'Imperial'
THEN UnitsofMeterRemoved = 'Metric'
END AS MeterModelRemoved
FROM CTE_2
But the Imperial is still showing up? Any ideas why this may be happening?
use below
SELECT * EXCEPT(UnitsofMeterRemoved),
CASE WHEN MeterModelRemoved = 'G4'
AND MeterManufacturerRemoved = 'KROMSCHRODER'
AND UnitsofMeterRemoved = 'Imperial'
THEN 'Metric'
ELSE UnitsofMeterRemoved
END AS UnitsofMeterRemoved
FROM CTE_2

Is there a way to ignore the AND in a CASE when something is true?

I have this Where clause
Select * From Student_Info si
Inner Join Certifications cc
Inner Join Cert_Earned ce
Where si.grad_date = #grad_date
AND cc.org_no = #org_no
but I need an additional AND that should be ignored if it turns out the value is false, I will want ALL certificates
AND cc.industrial = CASE WHEN #industrial = 0 THEN Do Nothing
Else #industrial
This would normally be expressed as:
AND (#industrial = 0 OR ccc.industrial = #industrial)
It sounds like you just want to add a predicate that does an OR between two different conditions
AND (#industrial = 0 or ccc.industrial = #industrial)
You can do something like this with CASE functions:
AND 1 = (CASE WHEN #industrial = 0 THEN 1
ELSE CASE WHEN cc.industrial = #industrial THEN 1 END
END)
or if cc.industrial is not nullable, then maybe:
AND cc.industrial = CASE WHEN #industrial = 0 THEN cc.industrial
Else #industrial END

SQL WHERE Clause True or False values

I have a table that contains a column showing whether an account is inactive.
The column values are either True or False... I
only want the False items.
So I've included a clause saying:
Where (account.inactive IN ("False"))
But I get a Operator/Operand type mismatch? error
I have been able to do other where clauses in this way but this one has me confused. Sorry if this is a newbie question - Thanks for the help.
Here is a copy of the query. It works fine except for the where clause:
SELECT
account.accountno,
account.title AS AccountName,
account.sortcode,
CAST(ICASE(account.sortcode = "A",10,
account.sortcode = "B",11,
account.sortcode = "C",13,
account.sortcode = "D",12,
account.sortcode = "E",14,
account.sortcode = "F",15,
account.sortcode = "K",20,
account.sortcode = "M",20,
account.sortcode = "N",20,
account.sortcode = "P",21,
account.sortcode = "Q",22,
account.sortcode = "R",23,
account.sortcode = "S",24,
account.sortcode = "L",30,
account.sortcode = "U",30,
account.sortcode = "V",30,
account.sortcode = "X",30,
account.sortcode = "Z",32,
account.sortcode = "^",33,
account.sortcode = "W",31,
account.sortcode = "Y",34,0) AS NUMERIC) AS GLCatCode,
account.inactive,
system.company AS CompanyName
FROM ((((account
LEFT JOIN trans
ON account.uniqueid = trans.accountid AND account.ledgerno=1 AND trans.trantype NOT IN (8) )
LEFT JOIN period
ON trans.periodno = period.periodno)
LEFT JOIN usercurr
ON account.cncycode = usercurr.code)
LEFT JOIN system
ON system.uniqueid = system.uniqueid)
Where (account.inactive IN ("False"))
GROUP BY
account.accountno,
AccountName,
account.sortcode,
account.inactive,
CompanyName,
GLCatCode
ORDER BY
account.sortcode , account.accountno
Are you sure the value can ever be 'FALSE'? Check the values available for filtering:
SELECT DISTINCT account.inactive
FROM ((((account
LEFT JOIN trans
ON account.uniqueid = trans.accountid AND account.ledgerno=1 AND trans.trantype NOT IN (8) )
LEFT JOIN period
ON trans.periodno = period.periodno)
LEFT JOIN usercurr
ON account.cncycode = usercurr.code)
LEFT JOIN system
ON system.uniqueid = system.uniqueid)
Change your WHERE Clause as follows?
Where (account.inactive = 'False')
I had a look at the data dictionary and it is logical type field with one character length. I tried a few different things and Where (account.inactive = 0) and it seems to work.
Best way to do is
Select /*+ your select creteria here, use decode() instead of CAST(ICASE) */
where condIsTrue = 'False'; --follow other things

Crystal Reports v14 not finding all null values

Alright, I've run into this before and have gotten everything to work correctly in the past. I have an SQL code that was created that now needs to be turned into a crystal report. The SQL shows 956 lines, but Crystal is only showing 886.
Here is the SQL code:
SELECT
I4240,
I4201,
I4202,
I4203,
I4204,
I4206,
I4213,
I4214,
I4225,
I4208,
I4299
FROM
MT.INVENTORY
WHERE
(
I4202 IN ('UNKNONWN','VERIFY MFR','OTHER','VARIOUS','TBD','NA','N/A') OR
(
I4203 IN ('UNKNONWN','VERIFY MODEL NUMBER','OTHER','VARIOUS','TBD','NA','N/A','MISCELLANEOUS')
OR I4203 IS NULL
OR LENGTH(I4203)=0
) OR
(
I4204 IN ('UNKNOWN DESCRIPTION','VERIFY DESCRIPTION','OTHER','VARIOUS','TBD','NONE - NO STD USED','NA','N/A','MISCELLANEOUS')
OR I4204 IS NULL
OR LENGTH(I4204)=0
)
) AND
I4240 NOT IN ('MT','STD','NESD')
ORDER BY I4240,I4202,I4203,I4204
and the record selection formula from CR:
(
{Inventory.I4240} <> 'mt' and
{Inventory.I4240} <> 'std' and
{Inventory.I4240} <> 'nesd'
)
AND
(
(
{Inventory.I4202} = 'UNKNONWN' OR
{Inventory.I4202} = 'VERIFY MFR' OR
{Inventory.I4202} = 'OTHER' OR
{Inventory.I4202} = 'VARIOUS' OR
{Inventory.I4202} = 'TBD' OR
{Inventory.I4202} = 'NA' OR
{Inventory.I4202} = 'N/A'
)
OR
(
{Inventory.I4203} = 'UNKNONWN' OR
{Inventory.I4203} = 'VERIFY MODEL NUMBER' OR
{Inventory.I4203} = 'OTHER' OR
{Inventory.I4203} = 'VARIOUS' OR
{Inventory.I4203} = 'TBD' OR
{Inventory.I4203} = 'NA' OR
{Inventory.I4203} = 'N/A' OR
{Inventory.I4203} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4203}) OR
LENGTH(trim({Inventory.I4203})) < 1 OR
INSTR(trim({Inventory.I4203}), "") = 0 OR
TRIM({Inventory.I4203}) = ""
)
OR
(
{Inventory.I4204} = 'UNKNOWN DESCRIPTION' OR
{Inventory.I4204} = 'VERIFY DESCRIPTION' OR
{Inventory.I4204} = 'OTHER' OR
{Inventory.I4204} = 'VARIOUS' OR
{Inventory.I4204} = 'TBD' OR
{Inventory.I4204} = 'NONE - NO STD USED' OR
{Inventory.I4204} = 'NA' OR
{Inventory.I4204} = 'N/A' OR
{Inventory.I4204} = 'MISCELLANEOUS' OR
ISNULL({Inventory.I4204}) OR
LENGTH(trim({Inventory.I4204})) < 1 OR
INSTR({Inventory.I4204}, "") = 0 OR
TRIM({Inventory.I4204}) = ""
)
)
Any help would be appreciated.
In Crystal, if a particular field can be null then you need to check for that condition as the very first thing you do with it, otherwise the entire formula will error out and nothing will be evaluated.
So in your case, fields I4203 and I4204 need the isnull() check moved to the top of their respective sections at the very least. If I4240 and I4202 can be null, then you should handle those conditions as well.
Also, you have a couple references to the word "UNKNONWN"; is that a typo?
The issue was that the report options were set to use null as default, but for some reason in the record selection formula it was set to exception. Once that was set to the default setting it worked without any issues and the counts were correct.

return string value from SQL Server tables where column type is integer

In my mssql tables i have a clomun that data type is integer. and when i was try thats you know the return value is int.
select h.hekimlik_kod, h.hekim_adi, h.hekim_soyadi, h.il_kod, h.ilce_kod, h.islem_tarihi, h.durum
h.asm, il.aciklama as il_adi, ilce.aciklama as ilce_adi from hekim as h
left join il_ilce as il on il.ust_kod = 0 and il.kod = h.il_kod
left join il_ilce as ilce on ilce.ust_kod = h.il_kod and ilce.kod = h.ilce_kod
int this sql statement "h.durum"' data type is integer.
but i want to make thats.
when h.durum is 0 return string is Yönetici
when h.durum 1 string is Normal Kullanıcı
how can i do in sql?
thank you for patience
Not sure if I understood the question, but try replacing h.durum on your SELECT with the following:
CASE WHEN h.durum = 0 THEN 'Yönetici'
WHEN h.durum = 1 THEN 'Normal Kullanıcı'
ELSE '???' END AS some_column_alias
Try this:
select
CASE
WHEN h.durum = 0 THEN N'Yönetici'
WHEN h.durum = 1 THEN N'Normal Kullanıcı'
END CASE ColumnName,
h.hekimlik_kod, h.hekim_adi, h.hekim_soyadi, h.il_kod, h.ilce_kod, h.islem_tarihi, h.durum
h.asm, il.aciklama as il_adi, ilce.aciklama as ilce_adi
FROM hekim as h
left join il_ilce as il on il.ust_kod = 0 and il.kod = h.il_kod
left join il_ilce as ilce on ilce.ust_kod = h.il_kod and ilce.kod = h.ilce_kod
;