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;
Related
I wanted when the below script do working, deleted special characters. But for first character in second parameter, this action is not working.
select translate('A##!a123','!##$%^&*)(_+',' ') from dual;
to replace ! with space, but did not remove from string.
One way is change your function to REGEXP_REPLACE
SELECT REGEXP_REPLACE('A##!a1+23', '[!##$%^&*)(_+]', '')
FROM dual;
I am removing that last 3 characters from the string "ABC123" using regexp_replace function in Oracle using the below statement
select REGEXP_REPLACE('ABC123','123','', LENGTH('ABC123') - 3) from dual;
The same result can be achieved in Postgres with the below statements,
select regexp_replace('ABC123','[123]', '','g')
select translate('ABC123','123', '');
Is there any way I can use the length function for replace as I have used in Oracle?
Why not simply use left()?
select left('ABC123', length('ABC123') - 3)
The same idea can be used in Oracle as well, but you need to use the substr() function. This should be more efficient in both databases.
You could also look into the trim functionality.
http://www.postgresqltutorial.com/postgresql-trim-function/
"select REGEXP_REPLACE('ABC123','123','', LENGTH('ABC123') - 3) from dual;"
would become
select ltrim('ABC123','ABC') from dual;
resulting in 123
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 '\'
I'm a newbie in regular expressions. I want to replace any text string symbols like (,),[,] to hyphens (for example):
SELECT REGEXP_REPLACE ('(<FIO>)', '(', '-') FROM dual;
This gives me ORA-12725 error.
Please, explain me, what's wrong?
Thanks.
To replace symbols, use the TRANSLATE function, it is less processor-intensive than regular expression functions:
SQL> SELECT translate ('(<FIO>)', '()[]', '----') replaced FROM dual;
REPLACED
--------
-<FIO>-
Regular expressions are more versatile and can do more complex things but are more expensive. In this case, replacing one character by another is done more efficiently by a specialized function. If you really want to use regular expressions, you could use REGEXP_REPLACE:
SQL> SELECT regexp_replace ('[(<FIO>)]', '[]()[]', '-', 1, 0) reg FROM dual;
REG
---------
--<FIO>--
Update: If you want to replace only the first symbol, translate won't work. Instead, use:
SQL> SELECT regexp_replace ('[(<FIO>)]', '[]()[]', '-', 1, 1) reg FROM dual;
REG
---------
-(<FIO>)]
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;