join two cases to create a new field - sql

I have a table with fields man_phone and head_phone, and in the selects there are such cases for obtaining them:
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m.man_l is not null then
m.man_phone
else
case
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
end
else
o.cont_phone
end as man_phone,
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
else
o.head_phone
end as head_phone
I want to add a new field called head_login with a case if man_phone = head_phone, then it will output man_l as head_login and I tried to push these two existing cases into only one case, what would be the right way to do this?
case
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m.man_l is not null then
m.man_phone
else
case
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
end
else
o.cont_phone
end as man_phone,
case
when p.dep_code <> 'MM' then
case
when p.man_id = '%%%%%%%' then
'Man Spec-100'
when m2.man_l is not null then
m2.man_phone
else
m3.man_phone
end
else
o.head_phone
end as head_phone
when p.department_code <> 'MM' then
case
when man_phone = head_phone
m2.man_l
else
m3.man_l
end
end as head_login

I want to add a new field called head_login with a case if man_phone = head_phone, then it will output man_l as head_login and I tried to push these two existing cases into only one case.
Do not try to merge the two cases into a third case. Evaluate them in a sub-query and then put the CASE which compares them into an outer query:
SELECT man_phone,
head_phone,
CASE
WHEN man_phone = head_phone
THEN man_l2
ELSE man_l3
END AS head_login
FROM (
SELECT CASE <your case> END AS man_phone,
CASE <your case> END AS head_phone,
m2.man_l AS man_l2,
m3.man_l AS man_l3
FROM table_name
)

Related

mismatched input '(' expecting <EOF>(line 3, pos 28)

My code looks like this, I do not know why it raising an error, the error is in line 3 after case when, Can anyone help on this? thanks
SELECT
CASE
WHEN
(
CASE
WHEN
(
ltrim(rtrim(status)) = 'CANCELLED'
AND ltrim(rtrim(COALESCE(iscancelledwithte, - ))) = '0'
)
THEN
CASE
WHEN
(
mincancellationdate IS NULL
)
THEN
CASE
WHEN
(
lastupdatedts IS NULL
)
THEN
'9999-12-31'
ELSE
lastupdatedts
END
ELSE
mincancellationdate
END
ELSE
CASE
WHEN
(
approvedforbillingts IS NULL
)
THEN
'9999-12-31'
ELSE
approvedforbillingts
END
END
)
= '9999-12-31'
THEN
status
ELSE
'Closed'
END
AS casestatusname
FROM
tblrequesttemp AS tblrequests
I've paste your sql request into SQL Syntax Checker (https://www.eversql.com/sql-syntax-check-validator/), and it return an error in your coalesce function. I believe you forget the quotes around the tiret character.
There is the fixed SQL Request:
SELECT
CASE
WHEN (
CASE
WHEN (Ltrim(Rtrim(status)) = 'CANCELLED' AND ltrim(rtrim(coalesce(iscancelledwithte, '-'))) = '0') THEN
CASE
WHEN (mincancellationdate IS NULL) THEN
CASE
WHEN (lastupdatedts IS NULL) THEN '9999-12-31'
ELSE lastupdatedts
end
ELSE mincancellationdate
end
ELSE
CASE
WHEN (approvedforbillingts IS NULL) THEN '9999-12-31'
ELSE approvedforbillingts
end
end) = '9999-12-31' THEN status
ELSE 'Closed'
end
AS casestatusname
FROM tblrequesttemp AS tblrequests

ORA-22835: Buffer too small for CLOB to CHAR or BLOB to RAW conversion (actual: 4448, maximum: 4000)

I am trying to Migrate client's data and this error happened during the migration procedure. I have searched around and the possible solution is to trim or use DBMS_LOB.SUBSTR, but i do not know where to put them in. It could be this part (TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval) but i am still new to SQL and not sure what could be the cause of the problem. Can anyone guide me on how to solve this problem? Thank you!
Update
AIMSCMDDL_AT.AI_OPENNET_SVC_RPT
SET
CUST_AGREEMENT_SIGNATURE =
CASE
WHEN TO_CHAR(CUST_AGREEMENT_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, CUST_DECLARATION_AUTH_ID =
CASE
WHEN CUST_DECLARATION_AUTH_ID = null
THEN null
ELSE
CASE WHEN LENGTH(CUST_DECLARATION_AUTH_ID)<>9
THEN TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
ELSE
CASE WHEN LENGTH(CUST_DECLARATION_AUTH_ID)=9
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,-1,1), '[A-Za-z]')
and AIMSCMDDL_AT.is_number(substr(CUST_DECLARATION_AUTH_ID,2,7)) = 1
THEN substr(CUST_DECLARATION_AUTH_ID,1,1)||LPAD(TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval),7,'0')||substr(CUST_DECLARATION_AUTH_ID,-1,1)
ELSE TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
END
END
END
, CUST_DECLARATION_AUTH_NM =
CASE
WHEN CUST_DECLARATION_AUTH_NM = null
THEN null
ELSE REGEXP_REPLACE(CUST_DECLARATION_AUTH_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, CUSTOMER_SIGNATURE =
CASE
WHEN TO_CHAR(CUSTOMER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, INSTALLER_NM =
CASE
WHEN INSTALLER_NM = null
THEN null
ELSE REGEXP_REPLACE(INSTALLER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, INSTALLER_SIGNATURE =
CASE
WHEN TO_CHAR(INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, REJ_CUSTOMER_ID =
CASE
WHEN REJ_CUSTOMER_ID = null
THEN null
ELSE
CASE WHEN LENGTH(REJ_CUSTOMER_ID)<>9
THEN TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
ELSE
CASE WHEN LENGTH(REJ_CUSTOMER_ID)=9
and REGEXP_LIKE(substr(REJ_CUSTOMER_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(REJ_CUSTOMER_ID,-1,1), '[A-Za-z]')
and AIMSCMDDL_AT.is_number(substr(REJ_CUSTOMER_ID,2,7)) = 1
THEN substr(REJ_CUSTOMER_ID,1,1)||LPAD(TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval),7,'0')||substr(REJ_CUSTOMER_ID,-1,1)
ELSE TO_CHAR(AIMSCMDDL_AT.BIOS_SEQ.nextval)
END
END
END
, REJ_CUSTOMER_NM =
CASE
WHEN REJ_CUSTOMER_NM = null
THEN null
ELSE REGEXP_REPLACE(REJ_CUSTOMER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, REJ_CUSTOMER_SIGNATURE =
CASE
WHEN TO_CHAR(REJ_CUSTOMER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
, REJ_INSTALLER_NM =
CASE
WHEN REJ_INSTALLER_NM = null
THEN null
ELSE REGEXP_REPLACE(REJ_INSTALLER_NM,'[[:alpha:]^[:digit:]^[:punct:]^]','*')
END
, REJ_INSTALLER_SIGNATURE =
CASE
WHEN TO_CHAR(REJ_INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
;
Most likely it comes from one of these expressions:
CASE
WHEN TO_CHAR(INSTALLER_SIGNATURE) = null
THEN null
ELSE 'NULL'
END
First, they don't work. ... = NULL never yields TRUE. Is IS NULL instead.
There is no need to use TO_CHAR(), IS NULL works for any data type.
And you can write it shorter as
INSTALLER_SIGNATURE = NVL2(INSTALLER_SIGNATURE, 'NULL', NULL)
NB,
REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,1,1),'[A-Za-z]')
and REGEXP_LIKE(substr(CUST_DECLARATION_AUTH_ID,-1,1), '[A-Za-z]')
can be written as
REGEXP_LIKE(CUST_DECLARATION_AUTH_ID,'^[A-Za-z].*[A-Za-z]$')

I am getting a missing keyword error in this case expression

I am getting a missing keyword error in this case expression.
What is the reason?
SELECT (CASE WHEN (MM.MAKE_DESCRIPTION = 'TOY')
THEN
CASE
WHEN MM.MODEL_DESCRIPTION = 'SCION' THEN MM.MAKE_DESCRIPTION = 'SCION'
WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION
END
ELSE MM.MAKE_DESCRIPTION END) AS MAKE
FROM table_make MM where value ='test';
It looks like in one of your when clauses you have put another Boolean expression as your THEN output so instead of saying then show 'SCION', you put then show Boolean expression which doesn't make sense.
SELECT (CASE WHEN (MM.MAKE_DESCRIPTION = 'TOY')
THEN
CASE
WHEN MM.MODEL_DESCRIPTION = 'SCION' THEN 'SCION' --edit here
WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION
END
ELSE MM.MAKE_DESCRIPTION END) AS MAKE
FROM table_make MM where value ='test';
Simplify it with an AND
CASE
WHEN MM.MAKE_DESCRIPTION = 'TOY' AND MM.MODEL_DESCRIPTION = 'SCION'
THEN 'SCION'
ELSE MM.MAKE_DESCRIPTION
END
your case statement missed two points
1.end will be after else
2.and another else will come for 1st case when
SELECT CASE WHEN MM.MAKE_DESCRIPTION = 'TOY'
THEN
CASE
WHEN MM.MODEL_DESCRIPTION = 'SCION' then
'SCION'
WHEN MM.MODEL_DESCRIPTION <> 'SCION' THEN MM.MAKE_DESCRIPTION
ELSE MM.MAKE_DESCRIPTION END
else null end AS MAKE -- this else for 1st case
FROM table_make MM where value ='test';

create sql view with case

I have a table named VWDRSSTA and it has the following fields
SYSTEM,
EREIGNIS;
DATUM_ZEIT,
ANTRAGSNUMMER,VORGANGSNUMMER,VERS_NR_INT,
DOK_ART,
DUNKEL
I am looking to create a view of this table with a filter in two fields DOK_ART and DUNKEL using CASE statement. Here is what I tried doing
CREATE VIEW VWDRSSTA_VIEW As
SELECT SYSTEM, EREIGNIS, DATUM_ZEIT, ANTRAGSNUMMER, VORGANGSNUMMER, VERS_NR_INT,
CASE
WHEN EREIGNIS = 'EIN-ES' AND DOK_ART = 'EN' Then 'EN'
ELSE ''
END
CASE
WHEN EREIGNIS = 'POL_AN' AND DUNKEL = 1 Then 1
ELSE ''
END
FROM VWDRSSTA;
Which is going wrong. How can I achieve the above?
Try this:
CREATE OR REPLACE FROCE VIEW VWDRSSTA_VIEW As
SELECT SYSTEM, EREIGNIS, DATUM_ZEIT, ANTRAGSNUMMER, VORGANGSNUMMER, VERS_NR_INT,
CASE
WHEN EREIGNIS = 'EIN-ES' AND DOK_ART = 'EN'
THEN 'EN'
ELSE ''
END AS DOK_ART,
CASE
WHEN EREIGNIS = 'POL_AN' AND DUNKEL = 1
THEN 1
ELSE ''
END AS DUNKEL
FROM
VWDRSSTA;

SQL stored procedure with CASE

Hello i have a quation with my stored procedure , i use 2 cases here , the first case it shows me right values and its OK, the second shows me only Null values in the field TirType , i don't understand what is the problem
CREATE VIEW dbo.YUITY
SELECT CAST(dbo.SC5116.CODE AS int) AS код, dbo.SC5116.DESCR AS Наименование,
CAST(dbo.SC3420.CODE AS int) AS TIR, dbo.SC3420.SP4947 AS Date,
CASE WHEN SC3420.SP4949 <> ' 0 ' THEN 'ПовышСтрах' ELSE 'ОснСтрах' END AS VID,
CASE WHEN dbo.SC3420.SP9214 = '714' THEN '4v' END AS TirType
FROM dbo.SC3420 INNER JOIN
dbo.SC5116 ON dbo.SC3420.SP3422 = dbo.SC5116.ID
WHERE (dbo.SC3420.SP4947 <> '01.01.1753')
Don't forget END after CASE. It will return NULL if no ELSE is specified and value is not '714'.
CREATE VIEW dbo.YUITY
AS
SELECT CAST(dbo.SC5116.CODE AS int) AS код, dbo.SC5116.DESCR AS Наименование,
CAST(dbo.SC3420.CODE AS int) AS TIR, dbo.SC3420.SP4947 AS Date,
CASE WHEN SC3420.SP4949 <> ' 0 ' THEN 'ПовышСтрах' ELSE 'ОснСтрах' END AS VID,
CASE WHEN dbo.SC3420.SP9214 = '714' THEN '4v' ELSE '' END AS TirType
FROM dbo.SC3420 INNER JOIN
dbo.SC5116 ON dbo.SC3420.SP3422 = dbo.SC5116.ID
WHERE (dbo.SC3420.SP4947 <> '01.01.1753')