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.
Related
In my SSRS report the query I'm using concats multiple strings based on value:
CONCAT(
CASE WHEN tu.TU_Ht_NREZ = 1 THEN 'Nichtraumzimmer, ' ELSE '' END,
CASE WHEN tuex.Doppelzimmer = 1 THEN 'Doppelzimmer, ' ELSE '' END,
CASE WHEN tuex.AllergikerBettwaesche = 1 THEN 'Allergiker Bettwäsche, ' ELSE '' END,
CASE WHEN tu.TU_Ht_VegEssen = 1 THEN 'Vegetarisch, ' ELSE '' END,
CASE WHEN tu.TU_Ht_SchwbEZ = 1 THEN 'Schwerbehindert, ' ELSE '' END,
CASE WHEN tuex.VeganesEssen = 1 THEN 'Veganes Essen, ' ELSE '' END,
CASE WHEN tuex.GlutenfreiseKost = 1 THEN 'Glutenfreie Kost, ' ELSE '' END,
CASE WHEN tuex.Rollstuhl= 1 THEN 'Rollstuhl' ELSE '' END
) AS TeilnehmerBemerkung
However, if the 'Rollstuhl' is not added to the string, then the whole string has comma ',' at the end.
How could I check if last character is ',' and replace it with '' ? I can't use STRING_AGG is not available in SQL Server 2014.
After the CASE is done, you could check the string if the last character is a comma and do some CASE-ing to replace it.
DECLARE #s NVARCHAR(100)= 'a,b,c,';
SET #s = CASE WHEN RIGHT(#s, 1) = ',' THEN SUBSTRING(#s,1,LEN(#s)-1) ELSE #s
END;
SELECT #s;
#s is your string with all the concatenation work.
I need to set def_where condition where its not working properly.
I am querying for North America in both reg_id_from and reg_id_to, but getting Asia along with North America.
this is the existing code example :
IF (:FREIGHT_ESTIMATES.DSP_REGION_FROM IS NOT NULL) THEN
def_where:=def_where||' reg_id_from in (select id from dss.regions where '||
' region like '''||:freight_estimates.DSP_region_from || ''''||
' and region_type = ''S'')';
ELSIF (:FREIGHT_ESTIMATES.DSP_REGION_TO IS NOT NULL) THEN
def_where:=def_where||' reg_id_to in (select id from dss.regions where '||
' region like '''||:freight_estimates.DSP_region_to || ''''||
' and region_type = ''S'')';
ELSIF (:FREIGHT_ESTIMATES.DSP_REGION_FROM IS NOT NULL) AND (:FREIGHT_ESTIMATES.DSP_REGION_TO IS NOT NULL) THEN
def_where:=def_where||' reg_id_from in (select id from dss.regions where '||
' region like '''||:freight_estimates.DSP_region_from || ''''||
' and region_type = ''S'') and '||' reg_id_to in (select id from dss.regions where '||
' region like '''||:freight_estimates.DSP_region_to || ''''||
' and region_type = ''S'')';
END IF;
problem is in your IF clause. The first condition is true (region_from is not null), trigger creates def_where for that case and does not check other conditions.
Try this
IF (:FREIGHT_ESTIMATES.DSP_REGION_FROM IS NOT NULL) THEN
def_where:=def_where||' reg_id_from in (select id from dss.regions where '||
' region like '''||:freight_estimates.DSP_region_from || ''''||
' and region_type = ''S'')';
END IF;
IF (:FREIGHT_ESTIMATES.DSP_REGION_TO IS NOT NULL) THEN
IF def_where IS NOT NULL THEN
def_where := def_where || ' and ';
END IF;
def_where:=def_where||' reg_id_to in (select id from dss.regions where '||
' region like '''||:freight_estimates.DSP_region_to || ''''||
' and region_type = ''S'')';
END IF;
this should work.
How about switching order of IF conditions so that the 3rd becomes 1st? "..." represent code you currently have, I didn't repeat it.
IF :FREIGHT_ESTIMATES.DSP_REGION_FROM IS NOT NULL AND
:FREIGHT_ESTIMATES.DSP_REGION_TO IS NOT NULL THEN
...
ELSIF :FREIGHT_ESTIMATES.DSP_REGION_FROM IS NOT NULL THEN
...
ELSIF :FREIGHT_ESTIMATES.DSP_REGION_TO IS NOT NULL THEN
...
END IF;
[EDIT]
On a second thought, why not even simpler option: declare two variables and set their values at the beginning of the script, using code you already use for FROM and TO options.
Then concatenate those variables into a single one, deciding only whether to put AND between them (if both of them exist) or not. Something like this:
def_where_from := 'reg_id_from in ...';
def_where_to := 'reg_id_to in ...';
def_where := case when :dsp_region_from is not null then
def_where_from
end
||
case when :dsp_region_from is not null and
:dsp_region_to is not null then ' and '
end
||
case when :dsp_region_to is not null then
def_where_to
end;
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",
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 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