PL/SQL Use Parameters Contains Quotation Marks - sql

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;

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;

How to handle/use special characters like percent (%) and ampersand (&) in Oracle SQL queries

I need to include the special character "%" in my LIKE clause in a SQL query, e.g.:
Select * From Some_Table Where Field_Name Like 'bla%bla&2';
How do I write that?
If you want to match Field_Name values that contain 'bla%bla&2', then you need to write this:
set define off
Select * From Some_Table Where Field_Name Like '%bla\%bla&2%' escape '\';
You get to specify which character you want to use to escape a following character (thanks should go to mathguy, not me). You also have to set define off to prevent sqlplus from trying to substitute values in a string.
If, however, you want to match Field_Name values that exactly equal the given string, then you do this instead:
set define off
Select * From Some_Table Where Field_Name = 'bla%bla&2';
If I am not mistakend you escape them with a backslash (\)
Select * From Some_Table Where Field_Name Like 'bla\%bla&2' ESCAPE '\';
Use escape \ to treat is a literal
SELECT *
FROM Some_Table
WHERE Field_Name LIKE 'blah\%'|| 'blah' ||'&'|| '2';
I'll guess that you're using a tool which treats &n, where n is a digit, as a variable marker. If you're using SQL*Plus or SQL Developer you'd need to issue the SQL*Plus command SET DEFINE OFF. Other tools may use other methods to accomplish this.
Best of luck.
I do not think the backslash escape character will work here for the ampersand. Instead, you will want to divide your search into concatenated strings using double pipes. Use single quotes around each piece of literal text. Next, replace the & with chr(38) which is the ampersand. You can see this with:
select chr(38) from dual
You will still want to include the backslash before the % and finish your statement with escape '\'. Notice, I did not quote the chr(38).
select * From Some_Table Where Field_Name Like 'bla\%bla'||chr(38)||'bla' escape '\'

Alternative to enquote_literal for string containing single quote

I am bound to use dbms_assert.enquote_literal to enquote string. The string is schema name which is unknown to me as it is coming as a parameter to my function. The only thing I know is that a schema name may contain single quote. For such strings enquote_literal fails with ORA-06502: PL/SQL: numeric or value error. Is there any alternative that I can use in place of enquote_literal that gives the same output as enquote_literal.
Not a good solution, but an easy solution is
REPLACE(dbms_assert.enquote_literal(REPLACE(text,'''','''''')),
'''''','''');
Input Text
hello'world
Output Text
'hello'world'
If you don't need the quote to appear even once
dbms_assert.enquote_literalreplace(text,'''',''));
Try 'q' quotes Read here.
select q'['<you string>']' from dual;
Demo
SQL> select q'['Hello'workddl']' Col from dual;
COL
--------------
'Hello'workddl'

Using LIKE pattern dynamically in execute format query in 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;

Select query that displays Joined words separately, not using a function

I require a select query that adds a space to the data based on the placement of the capital letters i.e. 'HelpMe' using this query would be displayed as 'Help Me' . Note i cannot use a stored function to do this the it must be done in the query itself. The Data is of variable length and query must be in SQL. Any Help will be appreciated.
Thanks
You need to use user defined function for this until MS give us support for regular expressions. Solution would be something like:
SELECT col1, dbo.RegExReplace(col1, '([A-Z])',' \1') FROM Table
Aldo this would produce leading space that you can remove with TRIM.
Replace regular expresion function:
http://connect.microsoft.com/SQLServer/feedback/details/378520
About dbo.RegexReplace you can read at:
TSQL Replace all non a-z/A-Z characters with an empty string
Assume if you are using Oracle RDBMS, you use the following,
REGEX_REPLACE
SELECT REGEXP_REPLACE('ILikeToWatchCSIMiami',
'([A-Z.])', ' \1')
AS RX_REPLACE
FROM dual
;
Managed to get this output: * SQLFIDDLE
But as you see it doesn't treat well on words such as CSI though.