create sql view with case - sql

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;

Related

join two cases to create a new field

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
)

I want to concatenate these case statements

I'm getting multiple results from these statements and want to concatenate these. Please help with syntax and structure
(SELECT NVL (
CASE WHEN (csi.hello = DT.S)
THEN 'Task IS ALREADY DONE'
WHEN VV.Mo LIKE NULL
THEN 'The model is not available'
WHEN (B.tsk = '7')
THEN 'The task status has been Cancelled'
WHEN (B.tski = '14')
THEN 'The task has been Assigned'
WHEN (B.tsko = '11001')
THEN 'The task has been Return Part STATUS'
END, '')RMA_CRITERIA,
Use string concatenation:
SELECT (CASE WHEN csi.hello = DT.S THEN 'Task IS ALREADY DONE;' ELSE '' END ||
CASE WHEN VV.Mo IS NULL THEN 'The model is not available;' ELSE '' END ||
CASE WHEN B.tsk = '7' THEN 'The task status has been Cancelled;' ELSE '' END ||
CASE WHEN B.tski = '14' THEN 'The task has been Assigned;' ELSE '' END ||
CASE WHEN B.tsko = '11001' THEN 'The task has been Return Part STATUS;' ELSE '' END
) as RMA_CRITERIA,
Note that this adds a separator (;), although you do not specify one. Also LIKE NULL always returns the equivalent of false; I assume you intend IS NULL. And the ELSE '' is redundant in Oracle, but it makes the intention clear.

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]$')

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')

Concatenating multiple CASE statements into one alias

After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.
I am trying to do something like this, but it is returning many rows, and eventually getting this error:
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
SELECT org.org_id,
org.org_name_1,
Datename(YEAR, member.enroll_date) AS enroll_year,
Max(CASE
WHEN board.member_from IS NULL THEN 0
ELSE 1
END) AS board_member,
CASE
WHEN ( org.delete_reason = 'OUT'
AND org.org_delete_flag = 'Y'
AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues > 0 ) THEN 'insurance_member|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues = 0
AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
ELSE ''
END + CASE
WHEN ( member.enroll_date IS NOT NULL
AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
ELSE ''
END + CASE
WHEN ( org.org_relationship_parent = 'Y'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
ELSE ''
END + CASE
WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'FREE'
AND org.org_status_flag = 'P' ) THEN 'associate_member|'
ELSE ''
END
--ELSE 'non_member'
--END
AS org_status,
60 AS expires_in,
CASE
WHEN stat.dues_type = 'M' THEN
CASE
WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
ELSE 0
END
ELSE
CASE
WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
ELSE member.payments
END
END AS dues_level,
CASE
WHEN ( org.affiliate_code = 'PCCE'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 1
ELSE 0
END AS pcce_membr,
-- '$'+CONVERT(VARCHAR,#dues) AS dues_level,
Ltrim(#product_level) AS product_level,
Ltrim(#involve_level) AS involvement_level
FROM organiz AS org
LEFT JOIN affilbil AS member
ON member.status_id = org.org_id
AND member.dues_category = 'MBR'
LEFT JOIN individu AS ind
ON ind.org_id = org.org_id
LEFT JOIN commembr AS board
ON board.status_id = ind.ind_id
AND board.committee_code = '5'
AND board.member_to IS NULL
LEFT JOIN statinsmorn AS stat
ON stat.status_id = org.org_id
AND stat.carrier = 'BS'
AND stat.planz = 'PCI'
WHERE org.org_id = #org_id
GROUP BY org.org_id,
org.org_name_1,
member.enroll_date,
org.delete_reason,
org.org_status_flag,
org.org_delete_flag,
stat.status_id,
stat.flat_dues,
stat.dues_type,
stat.termination_date,
org.org_misc_data_9,
org_relationship_parent,
org.dues_category,
member.status_flag,
member.dues_billed_annual,
member.payments,
stat.carrier,
org.Affiliate_Code
Well, this is embarrassing.
When I was making my changes to the stored procedure, I had inadvertently placed a call to the same procedure at the bottom. So I was recursively calling the same procedure over and over again. DOH.