Using LIKE pattern dynamically in execute format query in postgresql--9.5 - postgresql-9.5

I want to insert footext into LIKE pattern dynamically, but it gives error saying
ERROR: unrecognized conversion type specifier "'"........
This is my query:
RETURN QUERY EXECUTE format ('SELECT foocolumn
FROM footable
WHERE foocolumnother LIKE ''%L%'' ', footext);

Little late to the party, but according to Erwin's answer and the document, you can also try the ~ character of regex. So test out this
RETURN QUERY EXECUTE format ('SELECT foocolumn
FROM footable
WHERE foocolumnother ~ %L ', footext);

If you want to add a % into the format string you need to double it e.g. %F%%
%Lalso correctly adds single quotes so you don't need to put them in the format string.
However: using %F%% would generate 'bla'% - which is incorrect.
You need to add the wildcard to the variable not the format string:
RETURN QUERY EXECUTE format ('SELECT foocolumn
FROM footable
HERE foocolumnother LIKE %L' ', concat(footext, '%'));

Another late entry. Doubling both the single-quotes and percents works for me in Postgres 11.x:
l_query text := FORMAT('
SELECT e.gid, e.sessionname, e.shared, e.created, e.created_by
FROM exercise e
WHERE LOWER(e.created_by) LIKE ''%%%s%%'';'
, LOWER(p_user));
RETURN QUERY EXECUTE l_query;

Related

Oracle - How to use a function inside an escape with q

I know that I can escape strings in a statement like this:
select 'That''s a really funny ''joke''' from dual; --returns: That's a really funny 'joke'
Or like this:
select q'[That's a really funny 'joke']' from dual; --returns: That's a really funny 'joke'
Both are working fine.
Let's say I need to escape a string exactly in that select statement and I also need to use a function.
select q'[myfunction(somestringvariable)]' from dual;
Of course this only returns "myfunction(somestringvariable)"
As said, I need to escape the results of the function (can't be done inside the function, escape needs to happen in this select statement).
Can I use the function somehow inside a string escaped with "q"?
Thanks!
To double single quotes in the function result:
REPLACE(myfunction(somestringvariable), '''', '''''')
Oracle does not support template literals.
Just use:
SELECT myfunction(somestringvariable) FROM DUAL;
or, if the function does not return a string:
SELECT TO_CHAR(myfunction(somestringvariable)) FROM DUAL;
If you want to concatenate a quoted string literal and a function result then use string concatenation.
SELECT q'[That's a really funny 'joke']'
|| myfunction(somestringvariable)
FROM DUAL;

Character/String passed from java to stored procedure does not work

Hi I have a stored procedure which accepts parameter "Q_RES" from java. Its value is either "Y" or "N" . In my stored procedure I have a condition
If Q_RES = 'Y' THEN
sp_stat = 'TABLENAME.RES IN (' || Q_RES || ')';
END IF
nut I get error has ORA-00904: "Y" invalid Identifier
How can I fix this?
In PL/SQL assignment is Pascal style :=', not=`. Aside from that, I don't see any problem with the string concatenation shown here.
I suspect that sp_stat is being used later, in composing dynamic SQL. I would take a close look at the SQL being generated. It's probably goign to look like this:
... TABLENAME.RES IN (Y) ...
And if that is interpreted as SQL text, Oracle is going to see that bareword Y as an identifier, such as a column name, and attempt to resolve that. (If a table referenced in the query has a column named Y, the query wouldn't throw an error about an invalid identifier.
Just guessing here, but I suspect that we want Y to be a string literal. A string literal is enclosed in single quotes, like this:
... TABLENAME.RES IN ('Y') ...
To get a single quote inside a string literal, we can use two single quotes, e.g.
'I''m happy this is working'
Since we already know Q_RES is equal to 'Y', why not just do this:
sp_stat := 'TABLENAME.RES IN (''Y'')';
^^ ^^
or, if we want to reference Q_RES, enclose it in single quotes, like this:
sp_stat := 'TABLENAME.RES IN (''' || Q_RES || ''')';
^^ ^^

Show special characters in concat with db2 sql

I am running query like this for db2 sql
SELECT CONCAT(CONCAT('Order_no is =', Order_no), ' for line') FROM orders;
And result is coming like this:
Order_no is =123456 for line
But I want to fetch result as
Order_no is ='6640354' for line
I need to apply special characters to output, so can you please help me in this.
Use two single quotes together to escape a single quote:
SELECT CONCAT(CONCAT('Order_no is =''', Order_no), ''' for line')
FROM orders;
You can also use this;
select 'Order_no is=''' || trim(Order_no) || ''' for line' from orders;
Not sure why the use of nested CONCAT scalar is shown so pervasively in db2-tagged discussions, to concatenate more than one value.? Perhaps caused by how sometimes the documentation separates expressions and scalar functions, and in the latter docs might only offer a tiny Note: 'The CONCAT function is identical to the CONCAT operator. For more information, see "Expressions".'
I personally find the following use of the CONCAT operator, to be a much more readable way to compose the same character-string expression:
'Order_no is =''' CONCAT Order_no CONCAT ''' for line'
You can escape special character using \ or using another single quote like
select CONCAT( CONCAT('Order_no is =\'', Order_no), '\' for line') from orders;
Check DB2 documentation on Escaping special characters

How do I avoid Delphi interpreting special characters as parameters in SQL?

I want to run a Sql query with special characters in it. However, I don't want to use sql parameters. There is a way to run following query?
SqlString := 'Select * from Table1 where Name LIKE '`1234567890-=\]['';/.,<>?:"{}|+_)(*&^%$##!~%'
FSQLQuery.SQL.Clear;
FSQLQuery.SQL.Add( SqlString );
FSQLQuery.Open;
Delphi considers this query as Parameterised due to colon sign in
'`1234567890-=]['';/.,<>?:"{}|+_)(*&^%$##!~%'
and throws (No value for parameter '{}|+_)(*&^%$##!~%').
You should put something like this:
sqlString := 'SELECT * FROM Table1 WHERE Name LIKE ''`1234567890-=\]['''';/.,<>?:"{}|+_)(*&^%$##!~%'' ';
FSQLQuery.ParamCheck := False; //<<It MUST be prior than SetSql.
FSQLQuery.SQL.Clear;
FSQLQuery.SQL.Add( sqlString );
FSQLQuery.Open;
If FSQLQuery is TADOQuery then the easiest way to avoid this problem (colon char inside SQL.Tex) is to use
FSQLQuery.Connection.Execute(FSQLQuery.SQL.Text) instead of FSQLQuery.Open.
In this case you call ADO method directly without having Delphi parse your Sql.Text.
Attention to escaped chars inside SQL too, it is possible to resolve with QuotedStr function to works fine:
sqlString := 'SELECT * FROM Table1 WHERE Name LIKE ' +
QuotedStr('`1234567890-=\]['''';/.,<>?:"{}|+_)(*&^%$##!~%'' ) + ';';

PL/SQL Use Parameters Contains Quotation Marks

I wrote a procedure that included parameters type varchar2 and i pass from my application string is like that "'O','H','Y'" and my query is below.
select * from table_name t where t.productname in (parameter)
Query does not return any data. How to handle it?
I try yo double or three quatitions marks.
Try to set the parameter like this 'O,H,Y'
Here you can see some examples
If you are trying to pass a procedure as parameter then you should get nothing or error. You can only pass a function that returns a value as we all know. You need to add your code and samples to help you more. What exactly are you passing in ()?
-- Correct syntax to pass parameters in your case --
select * from table_name t where t.productname in ('O', 'H', 'Y')
/
To pass single quotes in an sql query, use single quotes twice. Simple!
You can have:
select * from table_name t where t.productname in ('''O''', '''H''', '''Y''')
To test this, you can use:
SELECT 'Schindler''s List' AS fname FROM dual;
and see that you can easily pass single quotes to a query.
Double quotes are not much of matter here. We can directly use double quotes inside an SQL string literal, like
SELECT 'He said, "Give me my teddy bear!"' AS dilogue FROM dual;