Doctrine Set Case When - orm

Is it possible to write in Doctrine the following SQL query?
UPDATE Inventory
SET
a = CASE WHEN a=id THEN NULL ELSE a END,
b = CASE WHEN b=id THEN NULL ELSE b END,
c = CASE WHEN c=id THEN NULL ELSE c END,
d = CASE WHEN d=id THEN NULL ELSE d END,
e = CASE WHEN e=id THEN NULL ELSE e END,
f = CASE WHEN f=id THEN NULL ELSE f END,
g = CASE WHEN g=id THEN NULL ELSE g END,
h = CASE WHEN h=id THEN NULL ELSE h END,
i = CASE WHEN i=id THEN NULL ELSE i END,
j = CASE WHEN j=id THEN NULL ELSE j END,
k = CASE WHEN k=id THEN NULL ELSE k END
WHERE userid=uid;

Nope, the CASE WHEN statement isn't supported in Doctrine - you could try using RAW SQL though ....

Related

Avoid calling a scalar function multiple times in a MERGE and UPDATE statement

I want to call function that return scalar multiple times.
How can I do that in a MERGE statement?
My code:
MERGE [dbo].MyTable m
USING (select * from Table2 edbl
where IsRowError = 0) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = case when dbo.func1(edbTable.Filed7) = 1 then 1
when dbo.func1(edbTable.Filed7) = 0 then 1
else 0 end,
m.Filed3 = case when dbo.func1(edbTable.Filed7) = 0 then 0 end,
m.Filed4 = case when dbo.func1(edbTable.Filed7) = 0 then 3 end,
m.Filed5 = case when dbo.func1(edbTable.Filed7) = 0 then 9 else 5 end,
m.Filed6 = getdate();
I want also to do it in UPDATE statement:
UPDATE md
set Filed1 = case when dbo.func1(edbl.Filed7) = 0 then 0 end,
Filed5 = case when dbo.func1(edbTable.Filed7)=0 then 9 else 5 end,
Filed6 = getdate()
from Table2 edbl
join Table3 m on edbl.Filed2= m.Filed2
join Table4 md on m.Filed3=md.Filed3
join Table5 mb on md.Filed4 = mb.Filed4
where IsRowError = 0
You can do it in the subquery:
MERGE [dbo].MyTable m
USING (select edbl.*, dbo.func1(edbTable.Filed7) as func1
from Table2 edbl
where IsRowError = 0
) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = (case when func1 = 1 then 1
when func1 = 0 then 1
else 0
end),
m.Filed3 = case when func1 = 0 then 0 end,
m.Filed4 = case when func1 = 0 then 3 end,
m.Filed5 = case when func1 = 0 then 9 else 5 end,
m.Filed6 = getdate();
However, it is not clear that this is a big win. If most rows do not match, this might not be a big improvement.
You could do it once inside USING part:
MERGE [dbo].MyTable m
USING (select *, dbo.func1(Filed7) AS f from Table2 edbl
where IsRowError = 0) edbTable
on edbTable.Filed1 = m.Filed1
WHEN MATCHED THEN UPDATE SET
m.Filed2 = case when f = 1 then 1
when f = 0 then 1
else 0 end,
m.Filed3 = case when f = 0 then 0 end,
m.Filed4 = case when f = 0 then 3 end,
m.Filed5 = case when f = 0 then 9 else 5 end,
m.Filed6 = getdate();
You can use APPLY with UPDATE statement :
UPDATE md
SET Filed1 = CASE WHEN fun = 0 THEN 0 END,
Filed5 = CASE WHEN fun = 0 THEN 9 ELSE 5 END,
Filed6 = getdate()
FROM Table2 edbl JOIN
Table3 m
ON edbl.Filed2 = m.Filed2 JOIN
Table4 md
ON m.Filed3 = md.Filed3 JOIN
Table5 mb
ON md.Filed4 = mb.Filed4 CROSS APPLY
( VALUES (dbo.func1(edbl.Filed7))
) edbl1(fun)
where IsRowError = 0;

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

If else ladder using Case in sql server

I am trying to write if elif ladder in SQL but I am finding it little difficult to do that. My ladder looks like below.
IF (MortgageProduct.FreeLegalFees != NULL) THEN
IF(MortgageProduct.FreeLegalFees==1) THEN
Return TRUE as FreeLegalFees
ELSE
Return FALSE as FreeLegalFees
ELSE
IF (ConveyancerFee!=NULL) THEN
IF ConveyancerFee.AMOUNT = 0.0 then
Return TRUE as FreeLegalFees
ELSE
Return FALSE as FreeLegalFees
END IF
ELSE
IF (LenderFee!=NULL) THEN
IF LenderFee.AMOUNT = 0.0 then
Return TRUE as FreeLegalFees
ELSE
Return FALSE as FreeLegalFees
END IF
ELSE
Return TRUE as FreeLegalFees
END IF
I know that I have to use Case for that in SQL but I am confuse in writing Case statement in sql. Can somebody please help me to frame this ladder in sql server.
This is far more succinct. I have also aliased what I assume are your table names:
CASE WHEN MP.FreeLegalFees IS NOT NULL THEN IIF(MP.FreeLegalFees = 1,1,0)
WHEN CF.AMOUNT IS NOT NULL THEN IIF(CF.AMOUNT = 0,1,0)
WHEN LF.Amount IS NOT NULL THEN IIF(LF.AMOUNT = 0,1,0)
ELSE 1 END
/*
MP MortgageProduct
CF ConveyancerFee
LF LenderFee
*/
I would write this as:
(case when mp.FreeLegalFees = 1 then 1
when mp.FreeLegalFees is not null then 0
when cf.amount = 0.0 then 1
when cf.amount is not null then 0
when lf.amount = 0.0 then 1
when lf.amount is not null then 0
else 1
end) as FreeLegalFees
CASE
WHEN MP.FREELEGALFEES = 1 THEN 1
WHEN MP.CONVEYANCERLEGALFEESET IS NOT NULL THEN
(
CASE
WHEN (
(SELECT AMOUNT FROM CONVEYANCERLEGALFEE WHERE LOCATION=#p_iLocation
and TYPEOFAPPLICATION=#p_iTypeOfApplication
and TYPEOFBUYER=#p_iTypeOfBuyer and MAXIMUMVALUE=100000
and CONVEYANCERLEGALFEESETSTRTDATE <= (SELECT GETDATE())
and CONVEYANCERLEGALFEESET = MP.CONVEYANCERLEGALFEESET ) = 0.0)
THEN 1
ELSE 0
END
)
WHEN ((Select COUNT(FEESET) FROM LEGALFEE WHERE TYPEOFAPPLICATION=#p_iTypeOfApplication
AND FEETYPE = (Select VALUEID from COMBOVALUE where VALUENAME='Redeeming')) >= 1 ) THEN
(
CASE
WHEN (
(SELECT AMOUNT FROM LEGALFEE
WHERE TYPEOFAPPLICATION = #p_iTypeOfApplication
AND FEETYPE= (SELECT VALUEID FROM COMBOVALUE WHERE VALUENAME='Redeeming')) = 0.0)
THEN 1
ELSE 0
END
)
ELSE 1
This is the solution which works perfect. Thank you all.
CASE
WHEN MortgageProduct.FreeLegalFees IS NOT NULL
THEN (
CASE
WHEN MortgageProduct.FreeLegalFees = 1
THEN 'TRUE'
ELSE 'FALSE'
END
)
WHEN ConveyancerFee IS NOT NULL
THEN (
CASE
WHEN ConveyancerFee.AMOUNT = 0.0
THEN 'TRUE'
ELSE 'FALSE'
END
)
WHEN LenderFee IS NOT NULL
THEN (
CASE
WHEN LenderFee.AMOUNT = 0.0
THEN 'TRUE'
ELSE 'FALSE'
END
)
ELSE 'TRUE'
END AS FreeLegalFees

SQL working strange

When I execute this SQL code:
select id, opis, vidrabota, tipprov, hitnost, valuta, drzava, zbirnaprov, tip_zbirprov, kanal
from dev_1450autoebanktip
where opis is null or Opis like case when isnull('','') = '' then Opis else '%' + '' + '%' end
and VidRabota = case when isnull('','') = '' then VidRabota else '' end
and TipProv = case when isnull(0,0) = 0 then TipProv else 0 end
and Valuta = case when isnull('','') = '' then Valuta else '' end
and drzava is null or Drzava = case when isnull('','') = '' then Drzava else '' end
I get this set of results:
But when I add one more condition (last row):
select id, opis, vidrabota, tipprov, hitnost, valuta, drzava, zbirnaprov, tip_zbirprov, kanal
from dev_1450autoebanktip
where opis is null or Opis like case when isnull('','') = '' then Opis else '%' + '' + '%' end
and VidRabota = case when isnull('','') = '' then VidRabota else '' end
and TipProv = case when isnull(0,0) = 0 then TipProv else 0 end
and Valuta = case when isnull('','') = '' then Valuta else '' end
and drzava is null or Drzava = case when isnull('','') = '' then Drzava else '' end
and KANAL = case when isnull(0,0) = 0 then KANAL else 0 end
I am losing one row in the result. What is causing this change?
Kanal is NULL in the last row.
and KANAL = case when isnull(0,0) = 0 then KANAL else 0 end
boils down to
and KANAL = KANAL
But this is not true for NULL, because NULL is the unknown value. When comparing null with null the result is neither true nor false, but unknown. Thus the added criteria dismisses the last record.
There is one thing I'd like to add: Use parentheses when mixing AND and OR. For instance
a = b or a = c and d = e
means
a = b or (a = c and d = e)
because AND has precedence over OR and you may want the expression to mean
(a = b or a = c) and d = e
Use parentheses in order to avoid any mistakes.

SQL add conversion of date to existing query

I'm would like to convert the date using this function. I'm using SQL server 2014
CONVERT(varchar, sc.StartDate,103) + '" + " " + "' + '" + " - " + "' + CONVERT(varchar, sc.EndDate,103) SIPDate
so it will display in dd/mm/yyyy - dd/mm/yyyy. How do i add it into the query? Thanks.
The query is:
SELECT CONCAT(sc.StartDate, sc.ENDDate) SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(sc.StartDate, sc.ENDDate)
You can just SELECT and GROUP BY this formatted date expression, i.e.:
SELECT CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103) AS SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONVERT(varchar, sc.StartDate, 103) + " - " + CONVERT(varchar, sc.EndDate, 103)
Try this. since your SQL SERVER is 2014 you can use format to format the date
SELECT CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy')) SIPDate,
COUNT (sj.LOComment) WeekReviewed,
COUNT(sjd.WeekNo) TotalWeek,
SUM(sjj.TotalDaysRecord) TotalDaysRecord,
COUNT(CASE sjd.JournalStatusCode WHEN 'D' THEN 1 ELSE NULL END) PENDingComplete
FROM StudentJournalDate sjd
LEFT JOIN StudentJournal sj
ON sjd.WeekNo = sj.WeekNo
LEFT OUTER JOIN
(
SELECT sj.WeekNo,
CASE WHEN RTRIM(sj.Day1Journal) = '' OR sj.Day1Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day2Journal) = '' OR sj.Day2Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day3Journal) = '' OR sj.Day3Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day4Journal) = '' OR sj.Day4Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day5Journal) = '' OR sj.Day5Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day6Journal) = '' OR sj.Day6Journal IS NULL THEN 0 ELSE 1 END +
CASE WHEN RTRIM(sj.Day7Journal) = '' OR sj.Day7Journal IS NULL THEN 0 ELSE 1 END AS TotalDaysRecord
FROM StudentJournal sj
) AS sjj
ON sjj.WeekNo = sj.WeekNo
LEFT OUTER JOIN StudentSIP sc
ON sc.AdminNo = sjd.AdminNo
GROUP BY CONCAT(FORMAT(sc.StartDate,'dd/MM/yyy'),+ ' - ' +FORMAT(sc.ENDDate,'dd/MM/yyy'))