SSRS - remove last character in string if character is comma ',' - reporting-services

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.

Related

Extract date & time from JSON data in mysql

I have the following data,
{"value": 0, "action": "UPDATED", "effectiveFrom": "2019-01-31T07:27:13.000Z"}
I am trying to extract the time value using the below SQL,
SELECT CASE
WHEN JSON_EXTRACT(changeData, '$.action') = 'UPDATED' THEN
time(replace(JSON_EXTRACT(changeData, '$.effectiveFrom'), 'T', ' '))
ELSE
'N'
END AS ACTION
FROM change
I used the replace function having read here that for mysql the 'T' should be replaced.
My sql is returning null.
You also need to replace double-quote and Z characters with '' or ' ' while replacing T with ' ' character for the time literal as
SELECT JSON_EXTRACT(changeData, '$.value') AS value,
CASE
WHEN JSON_EXTRACT(changeData, '$.action') = 'UPDATED'
THEN
TIME(
REPLACE(REPLACE(REPLACE(
JSON_EXTRACT(changeData, '$.effectiveFrom')
,'"',''),"Z",''),"T",' ')
)
ELSE 'N'
END AS action
FROM `change`
Demo

Oracle forms pre query def_where not working

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;

Format the SQL query to a variable

DECLARE #sql nvarchar(4000)
SET #sql='SELECT DISTINCT
WS.SIMNumber,
SMTP.SMTPMappingId,
CONVERT(VARCHAR(11),WS.ExpiryDate,106) as ExpiryDate,
CASE
WHEN BES.DisplayName IS NOT NULL THEN BES.DisplayName+'#'+SMTP.DomainName
ELSE
CASE WHEN BES.PIN IS NOT NULL THEN BES.PIN+'#'+SMTP.DomainName
ELSE '' END END AS EmailId,
CASE
WHEN (SELECT COUNT(*) FROM dbo.ExpiringEmailSimCardSent WHERE SimNumber=WS.SIMNumber AND ExpiryDate=WS.ExpiryDate)>0
THEN CONVERT(BIT,1)
ELSE CONVERT(BIT,0)
END AS IsEMailSent
FROM
WEBSERVICE_CACHE AS WS
LEFT OUTER JOIN
BES_SERVER_CACHE AS BES ON WS.SIMNumber = LEFT(BES.ICCID,19)
LEFT OUTER JOIN
BES_SMTP_Mapping AS SMTP ON BES.BESSqlServer = SMTP.BesServer
WHERE
CONVERT(DATETIME, GETDATE(), 109) > CONVERT(DATETIME, WS.ExpiryDate, 109)'
EXECUTE sp_executesql #sql
I have this SQL query want to convert it into a `nvarchar` variable because of # and '' . I am getting some errors
I am getting this errorMsg 102, Level 15, State 1, Line 9
Incorrect syntax near '#'.
If I rectify that it comes for another at # and ' '
You have to 'escape' your quotes:
This:
BES.PIN+'#'+SMTP.DomainName
Should be something like this:
BES.PIN+'''#'''+SMTP.DomainNam
experiment....

MYSQL view conditional insert

I have a mysql view up and running.
concat(`dp`.`fld_name`,
' ',
`mt`.`fld_model`,
' ',
`m`.`fld_diameter_nominal`) AS `fld_meter_type_info`,
now what i want to do is basic if else statement.
if(fld_type == 1 || fld_type == 4 ) -> get rid of fld_diameter_nominal in concat
else -> same concat
How to put this in view?
EDIT: SOLVED
concat(dp.fld_name,
' ',
mt.fld_model,
' ',
case when `m`.`fld_type` in (1,4)
then ''
else m.fld_diameter_nominal
end
) AS fld_meter_type_info,
Thanks for your time
concat(dp.fld_name,
' ',
mt.fld_model,
' ',
case when fld_type in (1,4)
then ''
else m.fld_diameter_nominal
end
) AS fld_meter_type_info
Try this::
Select
CASE WHEN fld_type in (1,4)
THEN oncat(`dp`.`fld_name`,' ',`mt`.`fld_model`)
ELSE
concat(`dp`.`fld_name`,' ', `mt`.`fld_model`,' ', `m`.`fld_diameter_nominal`) END AS `fld_meter_type_info`

MySQL CONCAT multiple fields with IF statement

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.