What is the meaning and difference between these queries?
SELECT U'String' FROM dual;
and
SELECT N'String' FROM dual;
(2) The U'' LiteralThe Oracle Call Interface (OCI) is the lowest level API that the rest of the client-side database access products use. It provides a flexible way for C/C++ programs to access Unicode data stored in SQL CHAR and NCHAR datatypes.
A string literal is a sequence of zero or more characters enclosed by single quotes. The null string ( '' ) contains zero characters. A string literal can hold up to 32,767 characters. PL/SQL is case sensitive within string literals. For example, PL/SQL considers the literals 'white' and 'White' to be different.
There are four kinds of literal values supported in SQL. They are : Character string, Bit string, Exact numeric, and Approximate numeric.
Literal strings are enclosed in single or double quotation marks. You can use literal strings just like you normally use a column name in the SELECT statement. The literal string will be displayed in very row of the query result.
In this answer i will try to provide informations from official resources
N''
is used to convert a string to NCHAR
or NVARCHAR2
datatype
According to this Oracle documentation Oracle - Literals
The syntax of text literals is as follows:
where
N
orn
specifies the literal using the national character set (NCHAR
orNVARCHAR2
data).
Also in this second article Oracle - Datatypes
The N'String'
is used to convert a string to NCHAR
datatype
From the article listed above:
The following example compares the
translated_description
column of thepm.product_descriptions
table with a national character set string:SELECT translated_description FROM product_descriptions WHERE translated_name = N'LCD Monitor 11/PM';
U''
is used to handle the SQL NCHAR String Literals in Oracle Call Interface (OCI)
Based on this Oracle documentation Programming with Unicode
The Oracle Call Interface (OCI) is the lowest level API that the rest of the client-side database access products use. It provides a flexible way for C/C++ programs to access Unicode data stored in SQL
CHAR
andNCHAR
datatypes. Using OCI, you can programmatically specify the character set (UTF-8, UTF-16, and others) for the data to be inserted or retrieved. It accesses the database through Oracle Net.
OCI is the lowest-level API for accessing a database, so it offers the best possible performance.
Handling SQL NCHAR String Literals in OCI
You can switch it on by setting the environment variable
ORA_NCHAR_LITERAL_REPLACE
toTRUE
. You can also achieve this behavior programmatically by using theOCI_NCHAR_LITERAL_REPLACE_ON
andOCI_NCHAR_LITERAL_REPLACE_OFF
modes inOCIEnvCreate()
andOCIEnvNlsCreate()
. So, for example,OCIEnvCreate(OCI_NCHAR_LITERAL_REPLACE_ON)
turns onNCHAR
literal replacement, whileOCIEnvCreate(OCI_NCHAR_LITERAL_REPLACE_OFF)
turns it off.[...] Note that, when the
NCHAR
literal replacement is turned on,OCIStmtPrepare
andOCIStmtPrepare2
will transformN'
literals withU'
literals in the SQL text and store the resulting SQL text in the statement handle. Thus, if the application usesOCI_ATTR_STATEMENT
to retrieve the SQL text from theOCI
statement handle, the SQL text will returnU'
instead ofN'
as specified in the original text.
From datatypes perspective, there is not difference between both queries provided
N'string'
just returns the string
as NCHAR
type.
U'string'
returns also NCHAR
type, however it does additional processing to the string
: it replaces \\
with \
and \xxxx
with Unicode code point U+xxxx
, where xxxx
are 4 hexadecimal digits. This is similar to UNISTR('string')
, the difference is that the latter returns NVARCHAR2
.
U'
literals are useful when you want to have a Unicode string independent from encoding and NLS settings.
Example:
select n'\€', u'\\\20ac', n'\\\20ac' from dual; N'\€' U'\\\20AC' N'\\\20AC' ----- ---------- ---------- \€ \€ \\\20ac
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With