Alternative to enquote_literal for string containing single quote - sql

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'

Related

Remove template text on regexp_replace in Oracle's SQL

I am trying to remove template text like &#x; or &#xx; or &#xxx; from long string
Note: x / xx / xxx - is number, The length of the number is unknown, The cell type is CLOB
for example:
SELECT 'H'ello wor±ld' FROM dual
A desirable result:
Hello world
I know that regexp_replace should be used, But how do you use this function to remove this text?
You can use
SELECT REGEXP_REPLACE(col,'&&#\d+;')
FROM t
where
& is put twice to provide escaping for the substitution character
\d represents digits and the following + provides the multiple occurrences of them
ending the pattern with ;
or just use a single ampersand ('&#\d+;') for the pattern as in the case of Demo , since an ampersand has a special meaning for Oracle, a usage is a bit problematic.
In case you wanted to remove the entities because you don't know how to replace them by their character values, here is a solution:
UTL_I18N.UNESCAPE_REFERENCE( xmlquery( 'the_double_quoted_original_string' RETURNING content).getStringVal() )
In other words, the original 'H'ello wor±ld' should be passed to XMLQUERY as '"H'ello wor±ld"'.
And the result will be 'H'ello wo±ld'

How we can display ASCII code for single quote in SQL?

I got the answer but can someone please explain me how and why we need to use extra single quote????
Select ASCII ('''') from dual;
Because you have to escape it.
string literal begins with a single quote
another one terminates the string ...
... unless it is immediately followed by another single quote which "escapes" it
For example: this is what you have:
SQL> Select ASCII ('''') from dual;
ASCII('''')
-----------
39
If you omit one of single quotes, you'll get an invalid string:
SQL> Select ASCII (''') from dual;
ERROR:
ORA-01756: quoted string not properly terminated
Only two single quotes? That's OK, but - it is an empty string, not what you wanted (a single quote):
SQL> Select ASCII ('') from dual;
ASCII('')
----------
Alternatively, instead of that many single quotes, you can use the q-quoting mechanism like this:
SQL> select ascii(q'[']') from dual;
ASCII(Q'[']')
-------------
39
SQL>
Basically, you'd enclose string (that contains single quotes) into brackets. That example (with only one single quote) looks stupid, but - have a look at this:
SQL> select 'L''Oreal' example1,
2 q'[L'Oreal]' example2 --> this
3 from dual;
EXAMPLE EXAMPLE
------- -------
L'Oreal L'Oreal
See? Within the brackets you use single quotes "normally", without escaping them. In more complex situations, that mechanism really, really helps.

How to determine if a variable has a special character using dual table on sql?

I'm currently trying to check if a string has a special character (value that is not 0 to 9 A to Z a to z), but the inhouse language that I'm currently using has a very limited function to do it (possible but it will take a lot of lines). but I am able to do a query on sql. Now I would like to ask if it is possible to query using the dual table on sql, My plan is to pass the string to variable and this variable will be use on my sql command. Thanks in advance.
Here is what you can use
SELECT REGEXP_INSTR('Test!ing','[^[:alnum:]]') FROM dual;
This will return a number other than 0 whenever your string has anything other than letters or numbers.
You can use TRANSLATE to remove all okay characters from the string. You get back a string containing only undesired characters - or an empty string when there are none.
select translate(
'AbcDefg1234%99.26éXYZ', -- your string
'.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'.') from dual;
returns: %.é

Aliases for sql column

I would like to understand why this does not work :
select my_table.my_column AS 'what I want to write'
from my_table
The error:
ORA-00923: FROM keyword not found where expected
How do I name my column what I want to write ?
Thank you
Single quotes are for string literals, double quotes are for delimited identifiers, e.g. "what I want to write".

How to remove part of the string in oracle

Input data:
abcdef_fhj_viji.dvc
Expected output:
fhj_viji.dvc
The part to be trimmed is not constant.
Use the REPLACE method
Select REPLACE('abcdef_fhj_viji.dvc','abcde','')
If you want this query for your table :
Select REPLACE(column,'abcde','') from myTable
For update :
UPDATE TABLE
SET column = REPLACE(column,'abcde','')
select substr('abcdef_fhj_viji.dvc',instr('abcdef_fhj_viji.dvc','_')+1) from dual
So, Its all depends on INSTR function, define from which position and which occurrence, you will get the index and pass that index to SUBSTR to get your string.
Since you didn't give a lot of information I'm gonna assume some.
Let's assume you want a prefix of some string to be deleted. A good way to do that is by using Regular Expressions. There's a function called regexp_replace, that can find a substring of a string, depending on a pattern, and replace it with a different string. In PL/SQL you could write yourself a function using regexp_replace, like this:
function deletePrefix(stringName in varchar2) return varchar2 is
begin
return regexp_replace(stringName, '^[a-zA-Z]+_', '');
end;
or just use this in plain sql like:
regexp_replace(stringName, '^[a-zA-Z]+_', '');
stringName being the string you want to process, and the ^[a-zA-Z]+_ part depending on what characters the prefix includes. Here I only included upper- and lowercase letters.