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;
Related
I have a section of code from an Oracle procedure. This parts works well as it is:
for r in (select * from json_table
(l_resp, '$.items[0].volumeInfo.authors[*]'
columns author varchar2(256) path '$'
)
)
loop
dbms_output.put_line('The value of Author ' || ' is: ' || r.author);
end loop r;
What I'd like to do is get the row number that is currently being processed. Something like a ROWNUM for the row "r".
I'd like to adjust the dbms_output line to look something like:
dbms_output.put_line('The value of Author ' || r.rownum || ' is: ' || r.author);
But the r.rownum doesn't work. How do I reference something like "r.rownum"?
I found a way.
For rowz in
(select rownum rn, j_auth_tab.*
from json_table(l_resp, '$.items[0].volumeInfo.authors[*]'
columns author varchar2(512) path '$') j_auth_tab)
Loop
dbms_output.put_line('Rownum: ' || rowz.rn || ' Author: ' || rowz.author);
End loop rowz;
That solved my problem.
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.
How to remove a character after the character ^ from a selected rows in table?
e.g.
TABLE Things
Boat
Do^2gs
Cat^fs
^KBear
Mi^&ce
D^Rice
RESULTS:
Boat
Dogs
Cats
Bear
Mice
Dice
select case when charindex('^', col) <> 0
then stuff(col, charindex('^', col), 2, '')
else col
end
-- to handle multiple ^ up to max of 4
select t.col,
r4.col
from Things t
cross apply
(
select col = case when charindex('^', col) <> 0
then stuff(col, charindex('^', col), 2, '')
else col
end
) r1
cross apply
(
select col = case when charindex('^', r1.col) <> 0
then stuff(r1.col, charindex('^', r1.col), 2, '')
else r1.col
end
) r2
cross apply
(
select col = case when charindex('^', r2.col) <> 0
then stuff(r2.col, charindex('^', r2.col), 2, '')
else r2.col
end
) r3
cross apply
(
select col = case when charindex('^', r3.col) <> 0
then stuff(r3.col, charindex('^', r3.col), 2, '')
else r3.col
end
) r4
-- UDF to remove the ^
create function remove_chr
(
#str varchar(100)
)
returns varchar(100)
as
begin
while charindex('^', #str) <> 0
begin
select #str = case
when charindex('^', #str) <> 0
then stuff(#str, charindex('^', #str), 2, '')
else #str
end
end
return #str
end
If you are using MySQL you could use:
SELECT col,
IF(INSTR(col,'^') > 0,CONCAT(LEFT(col,INSTR(col, '^')-1),
RIGHT(col,LENGTH(col) - INSTR(col, '^')-1)), col) AS result
FROM Things;
SqlFiddleDemo
And SQL Server equivalent:
SELECT col,
IIF(CHARINDEX('^',col) > 0,CONCAT(LEFT(col,CHARINDEX('^',col)-1),
RIGHT(col,LEN(col) - CHARINDEX('^',col)-1)), col) AS result
FROM Things
LiveDemo
SQL Server 2008:
SELECT col,
CASE WHEN CHARINDEX('^',col) > 0
THEN LEFT(col,CHARINDEX('^',col)-1) + RIGHT(col,LEN(col) - CHARINDEX('^',col)-1)
ELSE col
END AS result
FROM Things;
Keep in mind that it will work only if there is none or one occurence of ^.
Here is the solution which removes any number of occurrence of '^' .
I have created a function SQL server which is not used any loop or cursor.
CREATE FUNCTION [dbo].[FnReplaceChar](#pOriginalText VARCHAR(2000))
RETURNS VARCHAR(1000)
AS
BEGIN
DECLARE #vText VARCHAR(1000)
,#vXML XML
--Convert text as XML format
SELECT #vXML = '<Root><dtl><f>' + REPLACE(#pOriginalText,'^','</f></dtl><dtl><f>^')+'</f></dtl></Root>'
--Splits words started with '^' and combines after removing character starts with '^'
SET #vText = (
SELECT '' + ACT_TEXT
FROM
(
SELECT CASE WHEN CHARINDEX('^',DOC.COL.value('f[1]','VARCHAR(100)') ,0) > 0
THEN STUFF(DOC.COL.value('f[1]','VARCHAR(100)'),CHARINDEX('^',DOC.COL.value('f[1]','VARCHAR(100)') ,0),2,'')
ELSE DOC.COL.value('f[1]','VARCHAR(100)')
END AS ACT_TEXT
FROM #vXML.nodes('/Root/dtl') DOC(COL)
)T
FOR XML PATH('')
)
RETURN #vText
END
You can use this function in your select query
SELECT dbo.[FnReplaceChar](col_Name)
FROM [Things]
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`
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.