I need to emulate the insull() function from MySQL into pgAdmin but it doesn't seem to work.
What is the PostgreSQL equivalent for ISNULL()
I tried to follow the above link but it isn't producing the results as same as MySQL. Can someone shade some light on this please.
MySQL:
(case
when
((`s`.`Funding_Date` = '')
and (isnull(`s`.`Actual_Close_Date`)
or (`s`.`Actual_Close_Date` = '')))
then
'RPG_INV'
when
((isnull(`s`.`Funding_Date`)
or (`s`.`Funding_Date` <> ''))
and ((`s`.`Actual_Close_Date` = '')
or isnull(`s`.`Actual_Close_Date`)))
then
'Builder_Inventory'
else 'Owner_Inventory'
end) AS `Lot_Status`,
pgAdmin:
case when
Funding_Date = '' and (Actual_Close_Date is null or Actual_Close_Date= '')
then 'RPG Inventory'
when (Funding_Date is null or Funding_Date <> '')
and (Actual_Close_Date = '' or Actual_Close_Date is null)
then'Builder Inventory' else 'Owner Inventory'
end as "Lot Status",
Related
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.
How I can add some text on result of MySQL Case Operator?
I would like to get some result like this:
I try this but get a error syntax:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN ''
ELSE ''
END) AS job_url
FROM job
Probably you want to concatenate some strings? Then use the following query, where CONCAT is added to do the concatenation:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN CONCAT('')
ELSE CONCAT('')
END) AS job_url
FROM job
You should string concatenate the href string together.
SELECT (CASE WHEN job_url_outside IS NULL THEN '' ELSE '' END) AS job_url FROM job
Try use:
CONCAT(column,'some text',column)
More information here
In your case it will be like this:
SELECT (CASE
WHEN job_url_outside IS NULL
THEN CONCAT('')
ELSE CONCAT('')
END) AS job_url
FROM job;
DEMO here
I have a MySQL update statement which is attempting to reset two columns: label and PersTrCode. (This is in a Coldfusion program.) I'm doing this with CASE statements but I can't seem to get the syntax right -- I keep getting an error. The code:
<cfquery name = 'nonull' datasource = "Moxart">
update FinAggDb
set Label = CASE
WHEN PersActIncOutg = 'I' && PersTrCode IS NULL THEN 'Total Income'
WHEN PersActIncOutg = 'O' && PersTrCode IS NULL THEN 'Total Expense'
WHEN PersActIncOutg IS NULL && PersTrCode IS NULL THEN ' '
ELSE PersTrCode
END
SET PersTrCode = CASE
WHEN PersTrCode IS NULL THEN 'Total'
ELSE PersTrCode
END
</cfquery>
The error is the usual informative statement:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET PersTrCode = CASE WHEN PersTrCode IS NULL THEN 'Total' ELSE PersTrCode ' at line 8
Are multiple CASE statements not allowed? Or can someone tell me how to fix this?
An update statement has only one set clause, with the various columns you want to update separated by commas. Also, note that it's more common to use and and not &&, although both are valid in MySQL:
update FinAggDb
set Label = CASE
WHEN PersActIncOutg = 'I' AND PersTrCode IS NULL THEN 'Total Income'
WHEN PersActIncOutg = 'O' AND PersTrCode IS NULL THEN 'Total Expense'
WHEN PersActIncOutg IS NULL AND PersTrCode IS NULL THEN ' '
ELSE PersTrCode
END
, -- comma here, not a second "set" clause
PersTrCode = CASE
WHEN PersTrCode IS NULL THEN 'Total'
ELSE PersTrCode
END
I'm trying to do something I thought would be simple, but I'm stuck. I basically want to create a single address field from multiple address part fields, using an IF statement to use either an address or intersection. Here is my statement to make the field:
CONCAT(loc_name,'\n',
IF ( add_number != '' && add_street != '' ) THEN
CONCAT(add_number,' ',add_street,'\n')
ELSEIF ( x_street_1 != '' && x_street_2 != '' ) THEN
CONCAT(x_street_1,' & ',x_street_2,'\n')
END IF
,city,', ',
IF ( state != '') THEN
CONCAT(state,' ',country,'\n')
ELSEIF ( x_street_1 != '' && x_street_2 != '' ) THEN
CONCAT(country,'\n')
END IF
) AS loc_info
But it doesn't like what I am doing at all, it throws an error at:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') THEN \n\t\t\t\t\t\tadd_number,' ',add_street,'\n'\n\t\t\t\t\tELSEIF ( x_street_1 != '' && x_"
Which seems like it doesn't like my empty field ('') notation. But I don't know why. Can I not use the IF statement inside a CONCAT like that?
Thanks for any insight.
IIRC, the syntax you want to use is
IF(condition, expression_if_true, expression_if_false)
I could be wrong, but you might want to try that.
The syntax is not correct. You want to use CASE:
SET #loc_name = 'Location';
SET #add_street = 'Add Street';
SET #add_number = '10';
SET #x_street_1 = 'Street 1';
SET #x_street_2 = 'Street 2';
SET #city = 'City';
SET #state = 'State';
SET #country = 'Country';
SELECT Concat(#loc_name, '\n', CASE
WHEN #add_number != ''
AND #add_street != '' THEN
Concat(#add_number, ' ', #add_street, '\n')
WHEN #x_street_1 != ''
AND #x_street_2 != '' THEN
Concat(#x_street_1, ' & ', #x_street_2,
'\n')
end, #city, ', ', CASE
WHEN #state != '' THEN
Concat(#state, ' ', #country, '\n')
WHEN ( #x_street_1 != ''
AND #x_street_2 != '' ) THEN Concat(#country, '\n')
end) AS loc_info
Result
| LOC_INFO |
-----------------------------------------------
| Location
10 Add Street
City, State Country
|
Just find and replace # with .
this might also help:
CONCAT(loc_name,'\n',
IF ( add_number != '' && add_street != '' ,
CONCAT(add_number,' ',add_street,'\n'),
IF ( x_street_1 != '' && x_street_2 != '' ,
CONCAT(x_street_1,' & ',x_street_2,'\n'),""
)
),
city,
',' ,
IF ( state != '',
CONCAT(state,' ',country,'\n'),
IF ( x_street_1 != '' && x_street_2 != '' ,
CONCAT(country,'\n'),""
)
) AS loc_info
also what are you comparing here state != '' is it against null values?? if so this will give you incorrect answer you have to use state IS NOT NULL instead of that.
I'm trying to do this query which updates only the first column that is empty. Here is a query so far:
UPDATE `names` SET
`name_1` = CASE WHEN `name_1` = '' then 'Jimmy' else `name_1` end,
`name_2` = CASE WHEN `name_1` != '' and `name_2` = '' then 'Jimmy' else `name_2` end
It updates all of columns with 'Jimmy'. I think that that's because the SET will update it then move on to the next SET and will update that etc...Am I right on what's causing this? If so how could I fix this? If not how should I rewrite this?
I think if you swap the order, it will work properly.
Try this:
UPDATE `names` SET
`name_2` = CASE WHEN `name_1` != '' and `name_2` = '' then 'Jimmy' else `name_2` end,
`name_1` = CASE WHEN `name_1` = '' then 'Jimmy' else `name_1` end
I'm wondering if null values might be causing you problems. You could use the IFNULL function to convert them to empty strings (in case you have empty strings and NULLs). Try this:
UPDATE `names` SET
`name_1` = CASE WHEN IFNULL(`name_1`, '') = '' then 'Jimmy' else `name_1` end,
`name_2` = CASE WHEN IFNULL(`name_1`, '') != '' and IFNULL(`name_2`, '') = '' then 'Jimmy' else `name_2` end
or if you have all nulls:
UPDATE `names` SET
`name_1` = CASE WHEN `name_1` IS NULL then 'Jimmy' else `name_1` end,
`name_2` = CASE WHEN `name_1` IS NOT NULL and `name_2` IS NULL then 'Jimmy' else `name_2` end