Incorrect parameter count in the call to native function 'DATE_FORMAT' from Oracle to Mysql - mysql

I have Oracle script and I want to transform it from Oracle query into mysql query.
With the help of SQLines and documentation reading, I came up from Oracle Format :
nvl(TO_NUMBER(TO_CHAR(ADD_MONTHS(T156431.PERIOD_START, -6), 'yyyy'), '9999') , 1970) as c13
to Mysql Format :
ifnull(cast(DATE_FORMAT(TIMESTAMPADD(MONTH, -6, T156431.PERIOD_START), '%Y') as SIGNED), 1970) as c13
but when I try to run the mysql format, I endup with this error and I don't know what caused it :
Incorrect parameter count in the call to native function 'DATE_FORMAT'
So where did I go wrong?
Here is the full mysql script (The problem is at line 14)
SELECT
sum(T156033.PO_AMOUNT / 1000000.0) as c1,
sum(T156033.PO_AMOUNT / 1000.0) as c2,
sum(T156033.PO_AMOUNT / 1.0) as c3,
concat(concat(T156375.ACTIVITIES_R, ' '), T156375.ACTIVITIES_ALIAS_R) as c4,
T156375.ACTIVITIES_R as c5,
T156375.ACTIVITIES_ALIAS_R as c6,
T155910.AP_CODE_R as c7,
concat(concat(T156431.JSP_PLAN_ID_WO_FY, ' '), T156431.JSP_PLAN_NAME) as c8,
T156431.PERIOD_START as c9,
IFNULL(T155910.AP_CATEGORY_R , 'Unknown A&P Category') as c10,
IFNULL(case IFNULL(T155910.AP_CAT_COMM , 'UNKNOWN') when 'OPEX_AP' then 'Opex' when 'CAPE' then 'Capex' else 'Unknown' end , 'Unknown AP') as c11,
IFNULL(T156431.PLAN_TYPE , 'Unknown') as c12,
ifnull(cast(DATE_FORMAT(TIMESTAMPADD(MONTH, -6, T156431.PERIOD_START), '%Y') as SIGNED), 1970) as c13
FROM
(SELECT MFM_PROMOTION_HEADER.*, SUBSTRING(MFM_PROMOTION_HEADER.JSP_PLAN_ID,1, CASE WHEN LOCATE(MFM_PROMOTION_HEADER.JSP_PLAN_ID,'_') = 0 THEN CHAR_LENGTH(RTRIM(MFM_PROMOTION_HEADER.JSP_PLAN_ID)) ELSE LOCATE(MFM_PROMOTION_HEADER.JSP_PLAN_ID,'_') -1 END) AS JSP_PLAN_ID_WO_FY FROM MFM_PROMOTION_HEADER_VIEW MFM_PROMOTION_HEADER WHERE MFM_PROMOTION_HEADER.MARKET_CODE IN ('IT')) T156431,
(SELECT MARKET_CODE, AP_TREE, AP_TREE_R, AP_TREE_ALIAS, AP_TREE_ALIAS_R, AP_CATEGORY, AP_CATEGORY_R, AP_CATEGORY_ALIAS, AP_CATEGORY_ALIAS_R, AP_TYPE, AP_TYPE_R, AP_TYPE_ALIAS, AP_TYPE_ALIAS_R, AP_CODE, AP_CODE_R, AP_CODE_ALIAS, AP_CODE_ALIAS_R, FV_IND, AP_CAT_COMM, AP_CAT_COST FROM SGT_DIM_AP_CODE WHERE MARKET_CODE IN ('IT')) T155910,
(SELECT * FROM SGT_DIM_ACTIVITY WHERE MARKET_CODE IN ('IT')) T156375,
(SELECT RIGHT (REPEAT('0', 8) + LEFT ((CASE WHEN IFNULL(t2.customer, 'XXXX') = 'XXXX' THEN SUBSTRING (t1.CUSTOMER,1,LOCATE('_',t1.CUSTOMER)-1) ELSE SUBSTRING(t2.CUSTOMER,1,LOCATE('_',t2.CUSTOMER)-1) END), 8 ), 8 ) as customer_po, t1.*, (CASE WHEN t1.PO_EQUV_AMOUNT = 0 THEN 0 ELSE t1.PO_AMOUNT/t1.PO_EQUV_AMOUNT END) as ACTUAL_FX, ( CASE WHEN t1.PO_CURRENCY = 'RMB' THEN t1.PO_EQUV_AMOUNT ELSE t1.PO_AMOUNT/1.260924 END ) AS PO_AMOUNT_DF, t1.PO_EXCH_RATE * t1.PAYMENT_PAID_AMOUNT as PAYMENT_PAID_AMOUNT_HKD from mfm_po_jsp t1 left join MFM_CUSTOMER_MAPPING t2 on t2.market_code = t1.market_code and RIGHT((REPEAT ('0', 8)) + LEFT (t2.sub_ledger, 8) , 8 ) = RIGHT ((REPEAT ('0', 8)) + LEFT (SUBSTRING(t1.CUSTOMER,1,LOCATE('_',t1.CUSTOMER)-1), 8) , 8 ) ) T156033
WHERE T156033.JSP_PLAN_ID = T156431.JSP_PLAN_ID and T155910.AP_CODE = T156033.AP_CODE and T155910.MARKET_CODE = T156033.MARKET_CODE and T156033.MARKET_CODE = T156431.MARKET_CODE and T156033.ACTIVITIES = T156375.ACTIVITIES and T156033.MARKET_CODE = T156375.MARKET_CODE and YEAR(DATE_FORMAT(TIMESTAMPADD(MONTH,-6,T156431.PERIOD_START))) = 2021
GROUP BY T155910.AP_CODE_R, T156375.ACTIVITIES_ALIAS_R, T156375.ACTIVITIES_R, T156431.PERIOD_START, concat(concat(T156375.ACTIVITIES_R, ' '), T156375.ACTIVITIES_ALIAS_R), concat(concat(T156431.JSP_PLAN_ID_WO_FY, ' '), T156431.JSP_PLAN_NAME), IFNULL(T155910.AP_CATEGORY_R , 'Unknown A&P Category'), IFNULL(T156431.PLAN_TYPE , 'Unknown'), IFNULL(case IFNULL(T155910.AP_CAT_COMM , 'UNKNOWN') when 'OPEX_AP' then 'Opex' when 'CAPE' then 'Capex' else 'Unknown' end , 'Unknown AP')

Okay, so it seems the Error didn't came from the line I highlighted, but it came from another line.
From WHERE line
YEAR(DATE_FORMAT(TIMESTAMPADD(MONTH,-6,T156431.PERIOD_START))
and sure enough, it's missing some parameter to run the DATE_FORMAT Fuction, hence the error.

Related

Querying multiple databases using MSforeachDB

I'm querying about 60+ databases using MSforeachdb. The query works when I run it on a single database but I get a syntax error when I use MSforeachdb. Here is the code i used to query one database:
SELECT
DISTINCT(PAMARTID)
,PAMSTAID
, Gender
, DaysToNextApp
, DATEDIFF(day, DateofVisit, DateOfNextApp) AS date_difference
,DateOfNextApp
, DateofVisit
, MONTH(DateofVisit) AS Month
, DATENAME(month, DateofVisit) AS Month_Name
, YEAR(DateofVisit)AS Year
, REGID
, Description
, ROUND (DaysToNextApp/28, 0) AS MMD
, DATEDIFF(month, DateofVisit, DateOfNextApp) AS MMD_based_on_difference
FROM dbo.PAM p JOIN dbo.LuRegimen r ON p.PAMREMID = r.REGID
WHERE PAMSTAID = 2 AND (DateofVisit BETWEEN '2021-10-1 00:00:00:000' AND '2022-09-30 00:00:00:000')
I then adapted it for use on multiple database:
DECLARE #sql AS VARCHAR (4000)
SET #sql = 'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'')
EXECUTE ('' USE \[?\]
SELECT
DISTINCT(PAMARTID)
,PAMSTAID
, Gender
, DaysToNextApp
, DATEDIFF(day, DateofVisit, DateOfNextApp) AS date_difference
,DateOfNextApp
, DateofVisit
, MONTH(DateofVisit) AS Month
, DATENAME(month, DateofVisit) AS Month_Name
, YEAR(DateofVisit)AS Year
, REGID
, Description
, ROUND (DaysToNextApp/28, 0) AS MMD
, DATEDIFF(month, DateofVisit, DateOfNextApp) AS MMD_based_on_difference
FROM dbo.PAM p JOIN dbo.LuRegimen r ON p.PAMREMID = r.REGID
WHERE PAMSTAID = 2 AND (DateofVisit BETWEEN ''2021-10-1 00:00:00:000'' AND ''2022-09-30 00:00:00:000'')'')'
EXEC sp_MSforeachdb #command1 = #sql
however the above gives a the following error:
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near '2021'.
I removed the the following piece of code
AND (DateofVisit BETWEEN ''2021-10-1 00:00:00:000'' AND ''2022-09-30 00:00:00:000'')
from the sp-MSforeachdb query and it worked but if I add it again I get the aforementioned error
Any help or alternative methods would be appreciated.

User-defined function sorting column problem

I have taken reference from the internet about one user-defined function to locate 'nth occurrence of a string to do the sort column name in the database. I am using MySQL 5.5 version, not the latest version. Here is my sample database link https://dbfiddle.uk/?rdbms=mysql_5.5&fiddle=bcb32a6b47d0d5b061fd401d0888bdc3
My problem is I want to sort column name in the database follow the prefix number, but I am using below the SQL query, it doesn't work.
select t.id,t.name
from
(
select t.*, cast((case when col1_col2_ref > 0
then
substring_index(modified_name,'-',1)
else
modified_name
end
) as unsigned) col1
, cast((case when col1_col2_ref > 0
and col3_ref > 0
then
substr(modified_name,(col1_col2_ref + 1),(col3_ref - (col1_col2_ref + 1)))
when col1_col2_ref > 0
then
substr(modified_name,(col1_col2_ref + 1))
end) as unsigned) col2
, cast((case when col3_ref > 0
and col4_ref > 0
then
substr(modified_name,(col3_ref + 1),(col4_ref - (col3_ref + 1)))
when col3_ref > 0
then
substr(modified_name,(col3_ref + 1))
end) as unsigned) col3
, cast((case when col4_ref > 0
then
substr(modified_name,(col4_ref + 1))
end) as unsigned) col4
from
(
select t.*,substring_index(name,' ',1) modified_name
,locate('-',name,1) col1_col2_ref
,locate('/',name,1) col3_ref
,locate('/',name,locate('/',name,1)+1) col4_ref
from filing_code_management t
) t
) t
order by col1,col2,col3,col4
It shows me below the result, it cannot sort properly.
Output 1
Actually I want the output sample like below:
Output 2
Output 3
This is before I can sort the column name link, https://dbfiddle.uk/?rdbms=mysql_5.5&fiddle=6b12a4d42359cb30f27a5bfb9d0c8210. After I am inserted into new data, it cannot work for me. Maybe an example in new data like this error (R)100-6-2-2 Mesyuarat Majlis Kerajaan Negeri (MMKN) JKK if I put () in front. Or in new data like this error 100-1-1 Penggubalan/Penyediaan/Pindaan Undang-Undang/Peraturan if I put / in between the word.
Hope someone can guide me to solve this problem. Thanks.
You should be able to adapt the following code to your needs (tested at your DB Fiddle!). I've used the file_name column instead of the name column to slightly simplify building the sort fields, as it seems the file name is always repeated in the first part of the name field anyway.
This would be quite a bit simpler using regular expression support, but I note that the version of MySQL you are using doesn't have this feature (I think it arrives in SQL 8.0, if I'm not mistaken).
SELECT id,
num_hyphens,
CAST(SUBSTRING_INDEX(CONCAT(file_name_adj,'-'), '-', 1) AS UNSIGNED) AS sort1,
CAST(CASE WHEN num_hyphens = 0
THEN '0'
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(file_name_adj,'-', 2), '-',-1)
END AS UNSIGNED) AS sort2,
CAST(CASE WHEN num_hyphens <= 1
THEN '0'
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(file_name_adj,'-', 3), '-',-1)
END AS UNSIGNED) AS sort3,
CAST(CASE WHEN num_hyphens <= 2
THEN '0'
ELSE SUBSTRING_INDEX(file_name_adj, '-', -1)
END AS UNSIGNED) AS sort4,
file_name,
name
FROM (
SELECT id, name, MID(file_name, instr(file_name, ')') + 1) AS file_name_adj, file_name,
LENGTH(file_name) - LENGTH(REPLACE(file_name, '-', '')) AS num_hyphens
FROM filing_code_management
) t1
ORDER BY sort1, sort2, sort3, sort4

Missing Operator in query expression in MS Access

I am trying to run a query that returns letters before the space from an address column
SELECT col1,(Case When CHARINDEX(' ',address) = 0 then address Else LEFT(address,CHARINDEX(' ',address)-1) END) as streetNumber from table1
where stateID = 15
When I run this query in SQL Server Management Studio it runs but running it in Access gives me:
Syntax error (missing operator) in query expression '(Case WHEN
CHARINDEX(' ',address) =0 then address Else LEFT(address,CHARINDEX('
',address)-1) END)
But there are no operators missing. I am not sure why I am getting this error.
Use Access SQL and VBA:
Select
col1,
IIf(InStr([address], ' ') = 0, [address], Left([address], InStr([address], ' ') - 1)) As streetNumber
From
table1
Where
stateID = 15

Show blank or null values in my SSRS Report

I'm a newbie to SSRS Reporting Services. My report runs great except that it doesn't show null or blank cells. I know there are some. What should I add to this query to show the blank or null in the DistributionOwnerId? Thanks in advance!
SELECT Id,
CONVERT(varchar(10), DistributionDate, 101) AS DistributionDate,
DistributionDate AS OriginalDistributionDate,
MedDrugName AS DrugName,
MedDistributionSessionDescription AS Session,
DistributionOwnerId,
CASE a.[Status]
WHEN 0 THEN 'Success'
WHEN 1 THEN 'Refused By Inmate'
WHEN 2 THEN 'Inmate Did Not Show'
WHEN 3 THEN 'Inmate Not In Cell'
WHEN 4 THEN 'Security Lockdown'
WHEN 5 THEN 'Medication Held (State Reason)'
WHEN 6 THEN 'Medication Out Of Stock'
END AS Status,
Notes,
UserName,
(SELECT NoteText + ';' AS Expr1
FROM MARDistAddNotes AS c
WHERE (a.Id = MARDistributionId) FOR XML PATH('')
) AS AdditionalNotes
, InmateLastName
, InmateFirstName
, InmateNumber
FROM MARDistribution AS a
WHERE (Status > 0)
ORDER BY DistributionDate, Session
SELECT CONVERT (DATE, GETDATE()) 'Date Part Only'

MySql CASE expression with two input columns

I'm looking to use a case expression in mysql with two columns as input. This is how I would like to format the code. All code snippets use Set #a = 0; Set #b = 0; at the start, although those values can be modified. My own use case has several values each could be (more than 2 each).
SELECT
CASE (#a , #b)
WHEN (0 , 0) THEN 'Both zero'
WHEN (1 , 0) THEN 'a is 1'
WHEN (0 , 1) THEN 'b is 1'
WHEN (1 , 1) THEN 'both 1'
ELSE NULL
END;
When using the case statement this way, MySql throws Error Code: 1241. Operand should contain 1 column(s).
For reference, both of the following work, which makes me curious why the above doesn't.
SELECT
IF((#a , #b) = (0 , 0),
'Both zero',
IF((#a , #b) = (1 , 0),
'a is 1',
IF((#a , #b) = (0 , 1),
'b is 1',
IF((#a , #b) = (1 , 1), 'both 1', NULL))));
and
SELECT
CASE
WHEN (#a , #b) = (0 , 0) THEN 'Both zero'
WHEN (#a , #b) = (1 , 0) THEN 'a is 1'
WHEN (#a , #b) = (0 , 1) THEN 'b is 1'
WHEN (#a , #b) = (1 , 1) THEN 'both 1'
ELSE NULL
END;
So, the question:
Can I use a CASE expression like the first code snippet when doing a multi-column comparison?
If so, please provide an example. If not, why not?
The 3rd method is completely viable for my own use case, but if #a and #b were select statements, it wouldn't be.
The ideal would be a solution that:
Only runs inputs once, if they were select statements.
Can be easily expanded upon for any number of inputs.
Is agnostic to the input types (IE (#a,#b) = (0,'!##$:abc') is a valid case to check).
You can't use the short version of CASE with multiple values. You need to use the long version that tests both conditions with AND
CASE
WHEN #a = 0 AND #b = 0 THEN 'Both zero'
WHEN #a = 1 AND #b = 0 THEN 'a is 1'
WHEN #a = 0 AND #b = 1 THEN 'b is 1'
WHEN #a = 1 AND #b = 1 THEN 'Both 1'
ELSE NULL
END
Another option is to concatenate them.
CASE CONCAT(#a, #b)
WHEN '00' THEN 'Both zero'
WHEN '10' THEN 'a is 1'
WHEN '01' THEN 'b is 1'
WHWN '11' THEN 'Both 1'
ELSE NULL
END