How to select make the default value of all my variables '0' when only nulls are being returned? - sql

So I am running into issues where my conditional statements don't work because I am doing comparisons (at times for certain situations) where I'm comparing a null with an integer. I tried using an exception but after printing the variable values onto my display i found out that this exception only handled 'NO_DATA_FOUND' error messages. Is there a way I can get the variable to get a '0' selected into it in the case a 'NULL' or BLANK is returned? Below is my code:
SELECT MAX(VALUE)
INTO VARIABLE_1
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'HI';
SELECT MAX(VALUE)
INTO VARIABLE_2
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'BYE';
---
EXCEPTION
WHEN no_data_found
THEN SELECT 0
INTO VARIABLE_1
FROM DUAL;
SELECT 0
INTO VARIABLE_2
FROM DUAL;

You can use nvl() to provide a default value:
SELECT NVL(MAX(VALUE), 0)
INTO VARIABLE_1
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'HI';
SELECT NVL(MAX(VALUE), 0)
INTO VARIABLE_2
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'BYE';
The no_data_found exception won't be raised because you're using an aggregate function; that will always return one row, even its value is null. A null value is not the same as no rows being returned.

Or you can use coalesce
SELECT coalesce(MAX(VALUE), 0) AS value
INTO VARIABLE_1
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'HI';
SELECT coalesce(MAX(VALUE), 0) AS value
INTO VARIABLE_2
FROM VALUE V
JOIN REL_VALUE RV on V.value_id = RV.value_id
WHERE CODE = rCode
AND FIELD_TX = 'BYE';

use nvl command for it like :
nvl(column_name,0).

Related

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

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

ORA-01476: Divisor is equal to zero

I have this query that I've been given to fix and I cannot understand why its causing error. I reviewed similar topics involving the max function and the null function but I can't seem to make them work without throwing another error.
select case when count(trlr_num) > 0 then 'Inbounds Need Closed'
else 'All Good'
end as "Inbounds To Be Closed"
from (select t.trlr_num,
r.invnum "Invoice",
to_char(nvl(max(rl.init_rcv_dte), sysdate), 'MM/DD/YY HH:MI AM') as "Begin receive date",
round(sum(rl.idnqty) / sum(rl.expqty) *100, 1) as "% Received%"
from trlr t
join rcvinv r
on t.trlr_num = r.trknum
join rcvlin rl
on r.trknum = rl.trknum
and r.supnum = rl.supnum
and r.invnum = rl.invnum
and r.wh_id = rl.wh_id
join rcvtrk rt
on r.trknum = rt.trknum
and t.trlr_id = rt.trlr_id
and r.wh_id = rt.wh_id
where t.yard_loc_wh_id = 'US_3218'
and rt.rcvtrk_stat <> 'C'
and r.invtyp not in ('PKG', 'XFR')
and (rl.rcvsts <> 'QI' or (rl.rcvsts = 'QI' and t.trlr_seal1 is not null and t.trlr_seal2 is not null))
group by t.trlr_num,
r.invnum
having sum(rl.idnqty) / sum(rl.expqty) = 1
and nvl(max(rl.init_rcv_dte), sysdate) < sysdate -2)

sql oracle - filter query sql using case when

I have the below query
select
ens.ID_ENS,
ens.NOM_ENS,
-- for the first SURVEILLANT
disp.SALLE_EXAM,
disp.NB_HEURES_ENS,
-- for the second SURVEILLANT
disp.SALLE_EXAM2,
disp.NB_HEURES_ENS2
from ESP_ENSEIGNANT ens, ESP_MODULE_PANIER_CLASSE_SAISO disp
WHERE ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2) AND ens.NOM_ENS != 'A AFFECTER';
I want update the query for display SALLE_EXAM, NB_HEURES_ENS only for SURVEILLANT
select
ens.ID_ENS,
ens.NOM_ENS,
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN (disp.SALLE_EXAM AS "EXAM", disp.NB_HEURES_ENS AS "HOURDISP")
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN (disp.SALLE_EXAM2 AS "EXAM", disp.NB_HEURES_ENS2 AS "HOURDISP")
END
from ESP_ENSEIGNANT ens, ESP_MODULE_PANIER_CLASSE_SAISO disp
WHERE ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2) AND ens.NOM_ENS != 'A AFFECTER';
You can rewrite the query with explicit JOIN syntax as
SELECT ens.ID_ENS,
ens.NOM_ENS,
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN
disp.SALLE_EXAM
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN
disp.SALLE_EXAM2
END AS "EXAM",
CASE
WHEN ens.ID_ENS = disp.SURVEILLANT THEN
disp.NB_HEURES_ENS
WHEN ens.ID_ENS = disp.SURVEILLANT2 THEN
disp.NB_HEURES_ENS2
END AS "HOURDISP"
FROM ESP_ENSEIGNANT ens
JOIN ESP_MODULE_PANIER_CLASSE_SAISO disp
ON ens.ID_ENS IN (disp.SURVEILLANT, disp.SURVEILLANT2)
AND ens.NOM_ENS != 'A AFFECTER';
A side note : if disp.SURVEILLANT and disp.SURVEILLANT2 have equal values, then disp.SALLE_EXAM and disp.NB_HEURES_ENS will be picked respectively. Eg the first components have precedence.

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
;

PLSQL functions help

I have this SELECT below which i have to run it in a LOOP for every activity_id(record) that i get from the join.
Also,this SELECT has to be run in multiple places in a procedure to get details and for that records-->I run the assign_ course or assign_test as below.
Can anyone please help me write a function which would have this select .
inputs are strtplanid,strPersonid, p_ objective_id
FOR activity_id IN
(SELECT objact.activity_id, objact.activity_type,objact.IS_REQUIRED
FROM test_training_plan tp,
test_tp_objective tp_obj,
test_train_obj_activity objact
WHERE tp.tplan_id = tp_obj.tplan_id
AND tp.tplan_id = strtplanid
AND tp_obj.t_objective_id = p_objective_id
AND tp_obj.t_objective_id = objact.t_objective_id
AND objact.activity_id NOT IN (
SELECT tplplr.activity_id
FROM test_learning_record lr,
test_learning_record lr1,
test_tp_learning_activity tplplr
WHERE lr.lr_catalog_history_id = tplplr.activity_id
AND lr.learning_record_id =
tplplr.activity_lp_lr_id
AND tplplr.tp_lp_lr_id = lr1.learning_record_id
AND lr1.lr_catalog_history_id =
strtplanid
AND lr.lr_person_id = strPersonid
AND lr1.lr_person_id = strPersonid
AND lr.status IN
('PASSED', 'WAIVED', 'TESTED_OUT'))
AND objact.activity_id NOT IN (
SELECT event_id
FROM test_train_obj_activity toa,
test_event_sessions sessions,
test_learning_record lr1,
test_tp_learning_activity tplearnact,
test_learning_record tplr
WHERE toa.activity_id = sessions.event_id
AND sessions.event_session_id =
lr1.lr_catalog_history_id
AND lr1.learning_record_id =
tplearnact.activity_lp_lr_id
AND tplearnact.tp_lp_lr_id =
tplr.learning_record_id
AND tplr.lr_catalog_history_id =
strtplanid
--AND toa.is_required = 1
AND toa.t_objective_id = obj.t_objective_id
AND tplr.lr_person_id = strPersonid
AND lr1.lr_person_id = strPersonid
AND lr1.status IN
('PASSED', 'WAIVED', 'TESTED_OUT')))
LOOP
IF (activity.activity_type = 'Course')
THEN
ASSIGN_COURSETP(strPersonid,activity.activity_id,strPersonid,activity.activity_type,
activity.IS_REQUIRED,strtplanid,v_straccreditingorg);
ELSif (activity.activity_type ='Test')
THEN
ASSIGN_TESTTP(strPersonid,activity.activity_id,strPersonid,activity.activity_type,
activity.IS_REQUIRED,strtplanid,v_straccreditingorg);
END IF;
Does this need to return anything?
Why can't you wrap your code in the boilerplate function code?
CREATE OR REPLACE Function ACTIVITY_ASSIGN_TESTTP
(
strtplanid IN number
, strPersonid IN number
, p_objective_id IN number
)
RETURN number
IS
retVal number;
BEGIN
-- YOUR CODE
RETURN retVal
END ACTIVITY_ASSIGN_TESTTP;