When I am trying to execute INSERT statement in oracle, I got SQL Error: ORA-00917: missing comma error because there is a value as Alex's Tea Factory in my INSERT statement.
How could I escape ' ?
To escape it, double the quotes:
INSERT INTO TABLE_A VALUES ( 'Alex''s Tea Factory' );
In SQL, you escape a quote by another quote:
SELECT 'Alex''s Tea Factory' FROM DUAL
Instead of worrying about every single apostrophe in your statement.
You can easily use the q' Notation.
Example
SELECT q'(Alex's Tea Factory)' FROM DUAL;
Key Components in this notation are
q' which denotes the starting of the notation
( an optional symbol denoting the starting of the statement to be fully escaped.
Alex's Tea Factory (Which is the statement itself)
)' A closing parenthesis with a apostrophe denoting the end of the notation.
And such that, you can stuff how many apostrophes in the notation without worrying about each single one of them, they're all going to be handled safely.
IMPORTANT NOTE
Since you used ( you must close it with )', and remember it's optional to use any other symbol, for instance, the following code will run exactly as the previous one
SELECT q'[Alex's Tea Factory]' FROM DUAL;
you can use ESCAPE like given example below
The '_' wild card character is used to match exactly one character, while '%' is used to match zero or more occurrences of any characters. These characters can be escaped in SQL.
SELECT name FROM emp WHERE id LIKE '%/_%' ESCAPE '/';
The same works inside PL/SQL:
if( id like '%/_%' ESCAPE '/' )
This applies only to like patterns, for example in an insert there is no need to escape _ or %, they are used as plain characters anyhow. In arbitrary strings only ' needs to be escaped by ''.
SELECT q'[Alex's Tea Factory]' FROM DUAL
Your question implies that you're building the INSERT statement up by concatenating strings together. I suggest that this is a poor choice as it leaves you open to SQL injection attacks if the strings are derived from user input. A better choice is to use parameter markers and to bind the values to the markers. If you search for Oracle parameter markers you'll probably find some information for your specific implementation technology (e.g. C# and ADO, Java and JDBC, Ruby and RubyDBI, etc).
Share and enjoy.
Here is a way to easily escape & char in oracle DB
set escape '\\'
and within query write like
'ERRORS &\\\ PERFORMANCE';
Related
Let's say you have a table called Employee and one of the employee names begins or includes the [$] or [#] sign within the String, like Hel$lo or like #heLLo. How can you call the value?
Is it possible to select and trim the value in a single command?
Kind regards
If you want to select the names, but with special characters $ and # removed, you can use the TRANSLATE function. Add more characters to the list if you need to.
select translate(name, 'A$#', 'A') from employee;
The function will "translate" the character 'A' to itself, '$' and '#' to nothing (simply removing them from the string), and it will leave all other characters - other than A, $ and # - unchanged. It may seem odd that you need the 'A' in this whole business; and you really don't need 'A' specifically, but you do need some character that you want to keep. The reason for that is Oracle's idiotic handling of null; don't worry about the reason, just remember the technique.
You may need to remove characters but you don't know in advance what they will be. That can be done too, but you need to be careful not to remove legitimate characters, like the dot (A. C. Green), dash (John Connor-Smith), apostrophe (Betty O'Rourke) etc. You can then do it either with regular expressions (easy to write, but not the most efficient) or with TRANSLATE as above (it looks uglier, but it will run faster). Something like this:
select regexp_replace(name, [^[:alpha:].'-]) from employee
This will replace any character that is not "alpha" (letters) or one of the characters specifically enumerated (dot, apostrophe, dash) with nothing, effectively removing them. Note that dash has a special meaning in character classes, so it must be the last one in the enumeration.
If you need to make the changes in the table itself, you can use an update statement, using TRANSLATE or REGEXP_REPLACE as shown above.
I want to search a string using a delimiter which is a combination of 2 or more characters.
Please find the query i tried below:
select REGEXP_SUBSTR('123$#45$6$#789','[^$#]+',1,2) from dual
Required Output:
45$6
Output:
45
I understand it is easily possible using user defined functions [with INSTR+SUBSTR] however I am looking for an answer & explanation with REGEXP_SUBSTR.
Thanks in advance.
Not like that... Rather:
select REGEXP_SUBSTR('123$#45$6$#789','(.*?)(\$#|$)', 1, 2, null, 1) from dual;
Notice \$ in the code; $ means end of string, so if you mean a literal dollar sign symbol, you must escape it. This solution uses the "capturing group" concept - see the Oracle documentation for REGEXP_SUBSTR() if you are not familiar. The first capturing group is (.*?) - whatever comes before the delimiter; and it is referenced in the sixth (last) argument to REGEXP_SUBSTR.
Notice also that after the first capturing group I check for either the two-character delimiter or the end of the string... which is marked by $. Two options in parentheses and separated by | (another "special character") is the regular expression syntax for "either... or...".
I am trying to insert a space before the capital letters in oracle. I thought it would be easy using a regexp_replace, but I can't seem to get a proper back reference to the character I am replacing.
select trim(regexp_replace ('FreddyFox', '[A-Z]', ' \1' )) from dual;
Result: '\1reddy \1ox'
I have tried multiple variants of a back reference but I can't seem to find something that satisfies Oracle.
I did look at multiple SO answers but I could not figure out what is wrong.
e.g. regexp_replace: insert a space in a string if not already present
TRIM(regexp_replace ('FreddyFox', '([A-Z])', ' \1' ))
TRIM enables you to trim leading or trailing characters (or both) from a character string. If trim_character or trim_source is a character literal, then you must enclose it in single quotes. Default is both.
regexp_replace ('FreddyFox', '^([A-Z])', ' \1')
I hope there is no post limit since I have posted more than once today. :-P
Now I have a table in OracleSQL. I noticed there are some useless signs and want to delete them. The way I do it is to replace all of them. Below is my table and my query.
Here is my query:
SELECT
CASE WHEN WORD IN ('!', '"', '#','""') Then ''
ELSE WORD END
FROM TERM_FREQUENCY;
It is not giving me an error, but these special characters are not going away either... Any thoughts?
A little typo of yours: you use - instead of _
SELECT
CASE WHEN WORD IN ('!', '"', '#','""') Then ''
ELSE WORD END
-- FROM TERM-FREQUENCY; --This is where the problem is.
FROM TERM_FREQUENCY; -- Because your table is named TERM _ FREQUENCY
You originally tagged your question with 'replace' but then didn't use that function in your code. You're comparing each whole word to those fixed strings, not seeing if it contains any of them.
You can either use nested replace calls to remove one character at a time:
select replace(replace(word, '!', null), '"', null) from ...
... which would be tedious and rely on you identifying every character you didn't want; or you could use a regular expression only keep alphabetic characters, which I suspect is what you're really after:
select regexp_replace(word, '[^[:alpha:]]', null) from ...
Quick demo.
You might also want to use lower or upper to get everything into the same case, as you probably don't really want to count different capitalisation differently either.
i have a requirement to pull the column value containing special symbol('.').
i wrote the code like below
SELECT value, name_display_code
FROM vp40.ATTRIBUTES
WHERE attribute_type_id IN (
SELECT attribute_type_id
FROM vp40.attribute_types
WHERE name_display_code =
'ATTRIBUTE_TYPE.R'
|| '&'
|| 'D GMD NO'||'.')
i need value is ATTRIBUTE_TYPE.R&D GMD NO.
TL;DR: set define off before running your query (probably).
A period (.) is not a special character. The way you've split up and concatenated the value makes me wonder if you're actually seeing a substitution variable issue in SQL*Plus or SQL Developer (or maybe other another client) because of the &, which can in turn can make a . seem to disappear - though not in your specific case.
A period can mark the end of a substitution variable, and is needed if the next character is part of the string, so if you had:
select 'R&Ddept' from dual;
then the entire 'Ddept' would be treated as the substitution variable:
Enter value for ddept: D
old 1: select 'R&Ddept' from dual
new 1: select 'RD' from dual
'R
--
RD
To make just the D be the substitution variable and leave 'dept' as a fixed string, you delimit the variable with a period:
select 'R&D.dept' from dual;
Enter value for d: D
old 1: select 'R&D.dept' from dual
new 1: select 'RDdept' from dual
'RDDEP
------
RDdept
But if you want a period to actually be displayed as well, you need to add an extra one, to account for the one swallowed by the substitution processing:
select 'R&D..dept' from dual;
Enter value for d: D
old 1: select 'R&D..dept' from dual
new 1: select 'RD.dept' from dual
'RD.DEP
-------
RD.dept
Obviously this only applies if you actually want the substitution, and of course the & doesn't appear in the final string in any of those cases. More confusingly (for me) in your case, having a space between the & and the . means it is not treated as a delimiter anyway; for 'ATTRIBUTE_TYPE.R&D GMD NO.' only the &D is treated as a substitution and the . is left as it is. I'm guessing you got to this point with a shorter value.
What you probably want to achieve here is to avoid the substitution altogether. The way you're doing it, splitting up the string so the & is seen on its own and no substitution is seen, is certainly one way but painful, particularly if you don't necessarily control the string being used or there are multiple values.
There are at least two other ways; with set escape and set define:
set escape '\'
select 'ATTRIBUTE_TYPE.R\&D GMD NO.' from dual;
This defines an escape character for the SQL*Plus session, and you can then put that character before the ampersand - \& - so it is not treated as a substitution variable. But you still have to modify your original string, which isn't ideal.
set define off
select 'ATTRIBUTE_TYPE.R&D GMD NO.' from dual;
This disables substitution altogether, so the original string remains intact. This is the simplest approach generally. You can toggle it on and off as needed in your script; if you have a single statement that needs substitution in some places but also has ampersands that you want to keep you can either revert to the escape mechanism, or define a different substitution character, as long as you can find something else you'll never need to escape.
(You may also see references to set scan off; this predates set define and is obsolete, so don't use that in new code).