MYSQL view conditional insert - mysql

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`

Related

How do I split every data or string that has a space on it using MySQL?

I wrote a query which is this:
SELECT mrt_name as MRT ,
operation_alpha_numeric_codes as Original,
SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', 1) as First_code,
SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', -1) as Second_Code,
SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', -2) as Third_Code
FROM scraping.xp_pn_mrt;
I got result like this
As you can see the second_code copies the value of the original or the first_code if the value doesn't have a corresponding space or data. Also, the third code gets the second_code in the records that have a third code in them. How do I prevent the data being copied or set it to blank when the code doesn't have a corresponding value in it and how can I achieve getting the third code without copying the second one? Can someone help me with my query and what's wrong with it? Thanks a lot.
Since you're using MariaDB, you can use REGEXP_REPLACE to extract the parts of the code that you want:
SELECT
operation_alpha_numeric_codes as Original,
REGEXP_REPLACE(operation_alpha_numeric_codes, '^([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?$', '\\1') as First_code,
REGEXP_REPLACE(operation_alpha_numeric_codes, '^([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?$', '\\2') as Second_code,
REGEXP_REPLACE(operation_alpha_numeric_codes, '^([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?$', '\\3') as Third_code
FROM data
Output for (part of) your sample data
Original First_code Second_code Third_code
NS23 NS23
NS24 NE6 CC1 NS24 NE6 CC1
NS25 EW13 NS25 EW13
Demo on dbfiddle
Here's a version that will also work on MySQL 5.7, using RLIKE to check if the input matches given patterns:
SELECT
operation_alpha_numeric_codes as Original,
SUBSTRING_INDEX(operation_alpha_numeric_codes, ' ', 1) AS First_code,
CASE WHEN operation_alpha_numeric_codes RLIKE '^([^ ]+)$' THEN ''
WHEN operation_alpha_numeric_codes RLIKE '^([^ ]+)( ([^ ]+))?$' THEN SUBSTRING_INDEX(operation_alpha_numeric_codes, ' ', -1)
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes, ' ', 2), ' ', -1)
END AS Second_code,
CASE WHEN operation_alpha_numeric_codes RLIKE '^([^ ]+)( ([^ ]+)){2}$' THEN SUBSTRING_INDEX(operation_alpha_numeric_codes, ' ', -1)
ELSE ''
END AS Third_code
FROM data
Demo on dbfiddle
You can try the following:
SELECT
mrt_name as MRT ,
operation_alpha_numeric_codes as Original,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 1), ' ', -1) AS First_code,
If( length(operation_alpha_numeric_codes ) - length(replace(operation_alpha_numeric_codes , ' ', ''))>=1,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 2), ' ', -1) , '')
as Second_code,
If( length(operation_alpha_numeric_codes ) - length(replace(operation_alpha_numeric_codes , ' ', ''))>=2,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 3), ' ', -1), '')
AS Third_code
FROM scraping.xp_pn_mrt;
[EDIT]
For double spaces between each value, This will work:
SELECT
mrt_name as MRT ,
operation_alpha_numeric_codes as Original,
If( length(operation_alpha_numeric_codes ) - length(replace(operation_alpha_numeric_codes , ' ', ''))>=2,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 3), ' ', -1) , '')
as Second_code,
If( length(operation_alpha_numeric_codes ) - length(replace(operation_alpha_numeric_codes , ' ', ''))>=4,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 3), ' ', -1), '') AS Third_code
FROM scraping.xp_pn_mrt;
I have used CASE WHEN clause with LENGTH function.
I used LENGTH to calculate number of occurences of your separator ' ' in the string. CASE WHEN it is ONE occurances then there are TWO "results". CASE WHEN it is TWO occurances then there are THREE "results".
Here is the DEMO that will show the correct results for your two problematic data.
SELECT mrt_name as MRT
, operation_alpha_numeric_codes as Original
, SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', 1) as First_code
, CASE WHEN LENGTH(operation_alpha_numeric_codes) - LENGTH(REPLACE(operation_alpha_numeric_codes, ' ', '')) = 1
THEN SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', -1)
WHEN LENGTH(operation_alpha_numeric_codes) - LENGTH(REPLACE(operation_alpha_numeric_codes, ' ', '')) = 2
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', 2), ' ', -1)
ELSE NULL
END Second_Code
, CASE WHEN LENGTH(operation_alpha_numeric_codes) - LENGTH(REPLACE(operation_alpha_numeric_codes, ' ', '')) = 2
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', -2), ' ', -1)
ELSE NULL
END Third_Code
FROM scraping.xp_pn_mrt;

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

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.

Move matched string to end in MySQL

I am doing a GROUP_CONCAT to display names in the format of
Lastname1, Firstname1; Lastname2, Firstname2
There are occasions where the lastname also contains strings enclosed in curved brackets - ( and ). Since these will get displayed in the middle of the concatenated string I'm trying to move it to the end.
My solution so far is this:
GROUP_CONCAT(
DISTINCT
CASE
WHEN UPPER(psn.surname) LIKE '%INACTIVE%' THEN CONCAT(TRIM(REPLACE(psn.surname, '(Inactive)', '')), ', ', psn.firstname, ' (Inactive)')
ELSE CONCAT(psn.surname, ', ', psn.firstname)
END
ORDER BY
CASE
WHEN UPPER(psn.surname) LIKE '%INACTIVE%' THEN CONCAT(TRIM(REPLACE(psn.surname, '(Inactive)', '')), ', ', psn.firstname, ' (Inactive)')
ELSE CONCAT(psn.surname, ', ', psn.firstname)
END
ASC
SEPARATOR '; '
) AS contacts
So far this works but it only looks for a specific string, there are also cases when the string within the curved brackets isn't Inactive and I don't want to hard code all of those.
So basically how do I move a string enclosed in curved brackets to the end of a string. I imagine regex is the best solution to this problem but I don't know how to use regex.
Hmmm . . . something like this might work:
GROUP_CONCAT(DISTINCT (CASE WHEN psn.surname LIKE '%(%'
THEN CONCAT(TRIM(SUBSTRING_INDEX(psn.surname, '(', 1)), ', ',
psn.firstname, '('
SUBSTRING_INDEX(psn.surname, '(', -1)
)
ELSE CONCAT(psn.surname, ', ', psn.firstname)
END)
ORDER BY psn.surname, psn.firstname ASC SEPARATOR '; '
) AS contacts
I didn't repeat the expression for the order by. That seems like overkill.

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;

How can I write this MSSQL code in MySQL?

Hey guys I've written this MS SQL script with my awesome MS SQL skills, how can I get it to work in MySQL? It's failing on some of the string functions :( Don't know MySQL too well!
SELECT i.id as ID, e.code as EMPCODE, lower(e.email_address) as EMAIL
from load_incidents i , dim_employee e
WHERE
( (i.involvedemail = e.email_address)
or (replace(lower(e.preferred_name+e.last_name),' ', '') = replace(lower(i.involvedname),' ', ''))
or (replace(lower(e.preferred_name+e.last_name),' ', '') like replace(lower(i.involvedname),' ', '')+'%')
or (replace(lower(e.first_name+e.last_name),' ', '') = replace(lower(i.involvedname),' ', ''))
or (replace(lower(e.first_name+e.last_name),' ', '') like replace(lower(i.involvedname),' ', '')+'%')
or (replace(lower(e.employee_name),' ','') = replace(lower(i.involvedname),' ',''))
)
and charindex(' ',e.first_name) = 0
and (i.involvedemail= 'none available' or i.involvedemail is null or i.involvedemail = '')
UNION
SELECT i.id as ID, e.code as EMPCODE, lower(e.email_address) as EMAIL
from load_incidents i , dim_employee e
WHERE
( (i.involvedemail = e.email_address)
or (replace(lower(e.preferred_name+e.last_name),' ', '') = replace(lower(i.involvedname),' ', ''))
or (replace(lower(e.preferred_name+e.last_name),' ', '') like replace(lower(i.involvedname),' ', '')+'%')
or (replace(lower(left(e.first_name,charindex(' ',e.first_name)-1) + e.last_name),' ', '') = replace(lower(i.involvedname),' ',''))
or (replace(lower(e.employee_name),' ','') = replace(lower(i.involvedname),' ',''))
)
and charindex(' ',e.first_name) > 1
and (i.involvedemail= 'none available' or i.involvedemail is null or i.involvedemail = '')
YOUR problem is the charindex function, use locate (in place of charindex) for that purpose :
LOCATE(' ',e.first_name)
A lot of the functions are the same. Here's a list of MySQL string functions that you can use.
I tried Full Convert Enterprise with quite big success. Fast, easy and it did my job. Give it a try it may help you.
Also, You're probably best off doing this yourself to ensure everything is correct rather than relying on a third party tool (Also the additional benefit of understanding the differences between the two pieces of code). However you could use SQL to MySQL tool available here.