How to handle French character in Oracle insert statement [duplicate] - oracle

This question already has answers here:
PL/SQL, how to escape single quote in a string?
(5 answers)
Closed 10 months ago.
Insert into mytable (English_Name, French_Name)
Values('Contact Center', 'Centre d'appels')
This would not work since the special French Character d'.
Could someone help me?

The problem is caused by the single-quote character, since single-quote has special meaning - it is used to indicate the beginning and the end of a hard-coded text literal.
Simplest: you need to use TWO single-quote characters to generate one such character in the output (in this case: in the value stored in the table).
Cleaner: Use the q-quote mechanism (google for the term, if you had not heard of it before). Like this:
insert ... values ( ... , q'[Centre d'appels]')
Notice q'[ for opening and ]' for closing the text literal.

Related

Calling a value starting with $ character in Oracle

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.

Using regular expressions via `re-search-forward` in elisp [duplicate]

This question already has answers here:
Emacs - regular expressions in Lisp need to be double-escaped - why?
(4 answers)
Closed 4 years ago.
I want to search for an regular expression with the function re-search-forward
When I tried using the examples from the page
here: https://www.emacswiki.org/emacs/RegularExpression#toc1
specifically the regular expression \w\{20,\} used to search for a word with 20 letters or more, I get an error.
Here I am placing my cursor after the closing parenthesis in my Lisp buffer and pressing C-x C-e for evaluating it.
However, when I use the Regexp I-search via,
C-M-s it highlights the correct word as expected.
Why is this?
This regexp:
\w\{20,\}
is expressed in a double-quoted elisp string like so:
"\\w\\{20,\\}"
Backslashes are special to the double-quoted read syntax for strings as well as being special to regexp syntax; so if a backslash is for the regexp, you need to double it.

Add spaces before Capital Letters in Oracle

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')

Ruby regex include all alphabets, numbers and / [duplicate]

This question already has answers here:
How to escape all characters with special meaning in Regex
(2 answers)
Closed 7 years ago.
I know this might be asked time and again. But I'm really stuck with this. I've got it to work for including numbers and alphabets but I have no idea on how to include "/" also.
This is what I have,
name.gsub!(/[^0-9A-Za-z]/, '')
So if name is "Cool Stuff *(#/" it returns "CoolStuff". I'd just like it to return "CoolStuff/".
The / is a special character that must be 'escaped' (meaning to take the / literally, and not for a switch or special meaning). So you have:
name.gsub!(/[^0-9A-Za-z]/, '')
But also realize you could shorten your RegEx statement by making it case insensitive by adding an 'i' after the ending slash and therefore allowing you to drop either the [A-Z] or the [a-z] part. So you could have instead:
name.gsub!(/[^0-9A-Z\/]/i, '')
Hope this helps!

Oracle pl-sql escape character (for a " ' ")

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';

Resources