Using DB2 version 10.5. I have a database table which I know has at least one row with bad xml data, which is affecting some other queries I'm trying to do. One of the rows has a #x1A character in it. I'm trying to find a way to do a query that will give me all rows that have invalid xml characters [#x0E-#x1F] in them. Below are several attempts I've tried, but they did not work.
SELECT
e.EMLNAME
FROM MYDB.EMPLOYEE e
WHERE
-- [#x0E-#x1F]
/*
e.EMLNAME like '%#x0E%'
OR e.EMLNAME like '%#x0F%'
OR e.EMLNAME like '%#x10%'
OR e.EMLNAME like '%#x11%'
OR e.EMLNAME like '%#x12%'
OR e.EMLNAME like '%#x13%'
OR e.EMLNAME like '%#x14%'
OR e.EMLNAME like '%#x15%'
OR e.EMLNAME like '%#x16%'
OR e.EMLNAME like '%#x17%'
OR e.EMLNAME like '%#x18%'
OR e.EMLNAME like '%#x19%'
OR e.EMLNAME like '%#x1A%'
OR e.EMLNAME like '%#x1B%'
OR e.EMLNAME like '%#x1C%'
OR e.EMLNAME like '%#x1D%'
OR e.EMLNAME like '%#x1E%'
OR e.EMLNAME like '%#x1F%';
e.EMLNAME like '%\x0E%'
OR e.EMLNAME like '%\x0F%'
OR e.EMLNAME like '%\x10%'
OR e.EMLNAME like '%\x11%'
OR e.EMLNAME like '%\x12%'
OR e.EMLNAME like '%\x13%'
OR e.EMLNAME like '%\x14%'
OR e.EMLNAME like '%\x15%'
OR e.EMLNAME like '%\x16%'
OR e.EMLNAME like '%\x17%'
OR e.EMLNAME like '%\x18%'
OR e.EMLNAME like '%\x19%'
OR e.EMLNAME like '%\x1A%'
OR e.EMLNAME like '%\x1B%'
OR e.EMLNAME like '%\x1C%'
OR e.EMLNAME like '%\x1D%'
OR e.EMLNAME like '%\x1E%'
OR e.EMLNAME like '%\x1F%';
e.EMLNAME like '%\0x0E%'
OR e.EMLNAME like '%\0x0F%'
OR e.EMLNAME like '%\0x10%'
OR e.EMLNAME like '%\0x11%'
OR e.EMLNAME like '%\0x12%'
OR e.EMLNAME like '%\0x13%'
OR e.EMLNAME like '%\0x14%'
OR e.EMLNAME like '%\0x15%'
OR e.EMLNAME like '%\0x16%'
OR e.EMLNAME like '%\0x17%'
OR e.EMLNAME like '%\0x18%'
OR e.EMLNAME like '%\0x19%'
OR e.EMLNAME like '%\0x1A%'
OR e.EMLNAME like '%\0x1B%'
OR e.EMLNAME like '%\0x1C%'
OR e.EMLNAME like '%\0x1D%'
OR e.EMLNAME like '%\0x1E%'
OR e.EMLNAME like '%\0x1F%';
*/
e.EMLNAME like '%0x0E%'
OR e.EMLNAME like '%0x0F%'
OR e.EMLNAME like '%0x10%'
OR e.EMLNAME like '%0x11%'
OR e.EMLNAME like '%0x12%'
OR e.EMLNAME like '%0x13%'
OR e.EMLNAME like '%0x14%'
OR e.EMLNAME like '%0x15%'
OR e.EMLNAME like '%0x16%'
OR e.EMLNAME like '%0x17%'
OR e.EMLNAME like '%0x18%'
OR e.EMLNAME like '%0x19%'
OR e.EMLNAME like '%0x1A%'
OR e.EMLNAME like '%0x1B%'
OR e.EMLNAME like '%0x1C%'
OR e.EMLNAME like '%0x1D%'
OR e.EMLNAME like '%0x1E%'
OR e.EMLNAME like '%0x1F%';
Any pointers on how I can query for the hex values? I tried to do each hex value individually as I don't know of a way to do a range. If there is a way to do a range, that would be an even better solution.
So I got the following to work:
SELECT
e.EMLNAME
FROM MYDB.EMPLOYEE e
WHERE
e.EMLNAME like CONCAT(CONCAT('%', x'0E'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'0F'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'10'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'11'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'12'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'13'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'14'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'15'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'16'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'17'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'18'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'19'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'1A'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'1B'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'1C'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'1D'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'1E'), '%')
OR e.EMLNAME like CONCAT(CONCAT('%', x'1F'), '%');
Posting the answer, so others have an idea of what I'm looking for. I'll wait a while to see if anyone comes up with a cleaner solution. If they do, I'll accept that as the answer.
little variation of Venom
SELECT e.EMLNAME FROM MYDB.EMPLOYEE e
WHERE
e.EMLNAME like '%' || x'0E' || '%'
OR e.EMLNAME like '%' || x'0F' || '%'
OR e.EMLNAME like '%' || x'10' || '%'
OR e.EMLNAME like '%' || x'11' || '%'
OR e.EMLNAME like '%' || x'12' || '%'
OR e.EMLNAME like '%' || x'13' || '%'
OR e.EMLNAME like '%' || x'14' || '%'
OR e.EMLNAME like '%' || x'15' || '%'
OR e.EMLNAME like '%' || x'16' || '%'
OR e.EMLNAME like '%' || x'17' || '%'
OR e.EMLNAME like '%' || x'18' || '%'
OR e.EMLNAME like '%' || x'19' || '%'
OR e.EMLNAME like '%' || x'1A' || '%'
OR e.EMLNAME like '%' || x'1B' || '%'
OR e.EMLNAME like '%' || x'1C' || '%'
OR e.EMLNAME like '%' || x'1D' || '%'
OR e.EMLNAME like '%' || x'1E' || '%'
OR e.EMLNAME like '%' || x'1F' || '%';
Related
i have below tables and functions while executing the below function in postgresql ,it is showing ERROR: schema "tblmark" does not exist.Please help.
CREATE TABLE "LandXML_QCC_ParcelMarks"("DPID" TEXT,"From" TEXT,"Name" TEXT);
INSERT INTO "LandXML_QCC_ParcelMarks" VALUES ('1','ram','kumar');
CREATE TABLE "LandXML_QCC_ParcelInformation"("DPID" TEXT,"Pntref" TEXT)
INSERT INTO "LandXML_QCC_ParcelInformation" VALUES ('1','ram');
CREATE OR REPLACE FUNCTION GetParcelNonParcel(PlanID TEXT)
RETURNS TEXT AS $GetParcelNonParcel$
DECLARE
tblMark RECORD;
parCount INTEGER;
parNonCount INTEGER;
totalParNonCount TEXT;
tblCou INTEGER;
BEGIN
parCount=0;
parNonCount=0;
FOR tblMark IN (SELECT * FROM "LandXML_QCC_ParcelMarks" WHERE "DPID" = PlanID) LOOP
SELECT COUNT(*) INTO tblCou FROM "LandXML_QCC_ParcelInformation"
WHERE "DPID"=PlanID
AND (
"Pntref" LIKE '%' || tblMark.From || ',' || tblMark.Name '%'
OR
"Pntref" LIKE '%' || tblMark.Name || ',' || tblMark.From '%'
);
RAISE NOTICE 'Value: %', tblCou;
IF tblCou > 0 THEN
parCount = parCount + 1;
RAISE NOTICE 'Value: %', parCount;
ELSE
parNonCount = parNonCount + 1;
END IF;
END LOOP;
totalParNonCount = CAST(parCount AS TEXT) || ',' || CAST(parNonCount AS TEXT);
RAISE NOTICE 'Value: %', totalParNonCount;
RETURN totalParNonCount;
END;
$GetParcelNonParcel$ LANGUAGE plpgsql;
select GetParcelNonParcel('1');
While executing above function,it is showing ERROR: schema "tblmark" does not exist.
In your SELECT inside the FOR loop, you have the following condition:
... AND (
"Pntref" LIKE '%' || tblMark.From || ',' || tblMark.Name '%'
OR
"Pntref" LIKE '%' || tblMark.Name || ',' || tblMark.From '%'
)
You are missing the concatenation-operator || for the trailing '%' both times. Also, you are not properly referencing the columns of your record. Since you established them with " (double-quotes), you will always need to refer to them exactly the same way (This is a reason why you should not do this. So if it is not to late, change it to names without quotes.).
Using your momentary columnnames, this should help you:
... AND (
"Pntref" LIKE '%' || tblMark."From" || ',' || tblMark."Name" || '%'
OR
"Pntref" LIKE '%' || tblMark."Name" || ',' || tblMark."From" || '%'
)
I am trying to run the below query in a stored procedure and its not working.
We tried to print the query using NOTICE and we saw E gets appended to the regex and thats the reason the query doesnt show any output.
Not working
select order,version from mytable
where substring(version from quote_literal('[0-9]+\.[0-9]{1}'))
IN ('6.2') and order= 'ABC';
But the same query if i run from pgadmin query tool, it works fine.
Working
select order,version from mytable
where substring(version from '[0-9]+\.[0-9]{1}')
IN ('6.2') and order= 'ABC';
My requirement is to form the reqex dynamically in the stored procedure. Please guide on how to achieve this.
Below is the line of code in my stored procedure,
sql_query = sql_query || ' AND substring(version from ' || quote_literal( '[0-9]+\.[0-9]{1}' ) || ') IN (' || quote_literal(compatibleVersions) || ')';
raise notice 'Value: %', sql_query;
EXECUTE sql_query INTO query_result ;
and from notice i am getting the below output,
AND substring(version from E'[0-9]+\\.[0-9]{1}') IN ('{6.2}')
My requirement is to make this regex work.
I narrowed down to this query,
working
select substring(version from '[0-9]+\.[0-9]{1}') from mytable ;
not working
select substring(version from quote_literal('[0-9]+\.[0-9]{1}')) from mytable ;
Now i think its easy to fix it. You can try at your end also running this above queries.
Since your problem is not really the extended string literal syntax using E, but the string representation of the array in the IN list, your PL/pgSQL should look somewhat like this:
sql_query = sql_query ||
' AND substring(version from ' || quote_literal( '[0-9]+\.[0-9]{1}' ) ||
') IN (' || (SELECT string_agg(quote_literal(x), ', ')
FROM unnest(compatibleVersions
) AS x(x)) || ')';
quote_literal should be used in situations where u want to dynamically construct queries. In such situation quote_literal will be replaced by E in the final constructed query.
right way to use
select * from config_support_module where substring(firmware from '[0-9]+\.[0-9]{1}') IN ('6.2');
select * from config_support_module where substring(firmware from E'[0-9]+\.[0-9]{1}') IN ('6.2') ;
wrong usage of quote_literal in static queries
select * from config_support_module where substring(firmware from quote_literal('[0-9]+\.[0-9]{1}')) IN ('6.2') ;
This doesnt give you any errors/output.
quote_literal usage in dynamic queries
sql_query = sql_query || ' AND substring(version from ' || quote_literal( '[0-9]+\.[0-9]{1}' ) || ') ... .. ...
Is it possible to do something along the lines of:
define name1 = '23'
select * from my_db where value1 like '%' + name1 + %'
You are close. Here's a working example.
Use the variable command to register your variable. Then exec, which is a plsql block, to assign the value. Lastly use it in the select as a bind variable. As noted use || for concat operations not +
SQL> var name1 varchar2(20)
SQL> exec :name1 := '23';
PL/SQL procedure successfully completed.
SQL> select * from my_db where value1 like '%' || :name1 || '%';
Using the with statement allows you to inject the constant into the query.
WITH
name1 AS
(
SELECT 23 VALUE FROM DUAL
)
SELECT *
FROM MY_DB
WHERE VALUE1 LIKE '%' || (SELECT VALUE FROM NAME1) || '%'
;
What problem are you trying to solve by moving the constant out of the query?
This is my script:
select * from cliente
where NOME_CLIENTE like (Upper('&supplier_name') || '%');
and it all gives me "no rows selected".
Why does this happen? And how to fix it?
According to the description, you need to have the wildcard '%' before the user input as well, here is the example:
select * from cliente
where NOME_CLIENTE like ('%' || upper('&supplier_name') || '%');
I'm trying to do a dblink using a postgis function, but the linestring has given me many problems.
SELECT
DV3."XXX",
s."QQQID",s."X",s."Y",a."PPP"
FROM
"QQQ" s INNER JOIN "AAA" a ON a."QQQID" = s."QQQID",
dblink('dbname=ZZZ',
'SELECT XXX,the_geom
FROM "geometry", "QQQ" s
WHERE box2d(geomfromtext(''LINESTRING('|| s."X" ||' '||s."Y" ||','|| s."X" || ' '||s."Y" || ')'',2309))
&& the_geom'
)
DV3("XXX" INTEGER,"the_geom" geometry)
WHERE
contains(DV3.the_geom,geomfromtext('POINT('|| s."X" ||' '|| s."Y" ||')',2309))--21
AND "GGG" IS NOT NULL
ERROR
LINE 9: ... WHERE box2d(geomfromtext(''LINESTRING('|| s."X" + 0....
Following what Tanzeeb said, there are two instances of two single quotes right next to each other that look like they are supposed to be double quotes instead of two single quotes.
In
'SELECT XXX,the_geom
FROM "geometry", "QQQ" s
WHERE box2d(geomfromtext(''LINESTRING('|| s."X" ||' '||s."Y" ||','|| s."X" || ' '||s."Y" || ')'',2309))
&& the_geom'
Right before LINESTRING and right before ,2309.