I can't find the difference, although in all the searches they tell me the same thing but I can't fully break down its meaning to understand it. Data types referring to: ( https://msdn.microsoft.com/es-es/library/ms151817.aspx ) Oracle.
- NVARCHAR
- VARCHAR2
- VARCHAR
Oracle treats them grouped as follows in its documentation:
CHAR : fixed length
VARCHAR2 and VARCHAR : variable length
NCHAR and NVARCHAR2 : Unicode data
CHAR
The data type
CHAR
stores fixed-length character strings . When you create a table with one columnCHAR
, you must specify a string length (in bytes or characters) between 1 and 2000 bytes for the width of the columnCHAR
. The default value is 1 byte.Oracle warrants that:
When you insert or update a row in the table, the value for the CHAR column has the fixed length .
If you give a shorter value, then the value is padded blank to the fixed length.
If a value is too large, Oracle Database returns an error.
The Oracle database compares the values of
CHAR
using the blank-padded comparison semantics .VARCHAR2
The data type
VARCHAR2
stores variable-length character strings . When you create a table with a columnVARCHAR2
, you specify a maximum string length (in bytes or characters) between 1 and 4000 bytes for the columnVARCHAR2
. For each row, Oracle Database stores each value in the column as a variable-length field unless a value exceeds the maximum column length, in which case Oracle Database returns an error. The use ofVARCHAR2
andVARCHAR
saves space in the table.For example, suppose you declare a column
VARCHAR2
with a maximum size of 50 characters. In a single-byte character set, if only 10 characters are given for the column valueVARCHAR2
in a particular row, the row's column stores only the 10 characters (10 bytes) and not 50 .Oracle Database compares the values of
VARCHAR2
using unsupported comparison semantics .VARCHAR
The data type
VARCHAR
is synonymous with the VARCHAR2 data type. To avoid possible changes in behavior, always use the data typeVARCHAR2
for storing variable-length character strings .Regarding the semantics of length for character data types
Globalization support allows the use of multiple character sets for character data types. Globalization support allows you to process single-byte and multibyte character data and convert between character sets. Client sessions can use client character sets that are different from the database character set.
Consider character size when specifying column length for character data types. You should be aware of this issue when estimating space for tables with columns that contain character data.
The length semantics of character data types can be measured in bytes or characters.
Byte semantics treat strings as a sequence of bytes. This is the default value for character data types.
Character semantics treat strings as a sequence of characters. A character is technically a codepoint from the database character set.
For single-byte character sets, the columns defined in character semantics are basically the same as those defined in byte semantics. Character semantics are useful for defining variable - width multibyte strings ; Reduces complexity by defining the actual length requirements for data storage.
For example, in a Unicode (UTF8) database, you must define a column
VARCHAR2
that can store up to five Chinese characters along with five English characters. In byte semantics, this would require (5 * 3 bytes) + (1 * 5 bytes) = 20 bytes; In character semantics, the column would need 10 characters.VARCHAR2 (20 BYTE) y SUBSTRB (<cadena>, 1, 20)
they use byte semantics.VARCHAR2 (10 CHAR) y SUBSTR (<cadena>, 1, 10)
they use character semantics.The parameter
NLS_LENGTH_SEMANTICS
decides whether a new character data type column uses byte or character semantics. The default semantic length is byte. If all character data type columns in a database use byte semantics (or all use character semantics), users do not have to worry about which columns use which semantics. The qualifiersBYTE
andCHAR
shown above should be avoided where possible, as they lead to mixed semantic databases . Instead, the initialization parameterNLS_LENGTH_SEMANTICS
must be set correctly in the server parameter file (SPFILE
) or in the initialization parameter file, and the columns must use default semantics.NCHAR and NVARCHAR2
NCHAR
andNVARCHAR2
are Unicode data types that store Unicode character data . The character set of data typesNCHAR
andNVARCHAR2
can only beAL16UTF16
orUTF8
and is specified at database creation time as the national character set.AL16UTF16
andUTF8
they are Unicode encoding.The data type
NCHAR
stores fixed-length character strings that correspond to the national character set.The data type
NVARCHAR2
stores variable-length character strings.When you create a table with a column
NCHAR
orNVARCHAR2
, the specified maximum size is always in character-length semantics. Character-length semantics are the default, single-length semantics for NCHAR orNVARCHAR2
.For example, if the national character set is UTF8, the following statement defines the maximum byte length of 90 bytes:
This statement creates a column with a maximum character length of 30. The maximum byte length is the multiple of the maximum character length and the maximum number of bytes in each character.
The maximum length of a column
NCHAR
is 2000 bytes. It can contain up to 2000 characters. Actual data is subject to the maximum byte limit of 2000. The two size constraints must be satisfied simultaneously at run time.The maximum length of a column
NVARCHAR2
is 4000 bytes. It can contain up to 4000 characters. Actual data is subject to the maximum byte limit of 4000. The two size constraints must be satisfied simultaneously at run time.See also: Oracle Database Globalization Support Guide for more information on data types
NCHAR
andNVARCHAR2
Using Unicode data in Oracle Database
Unicode is an effort to have a unified encoding of every character in every language known to man. It also provides a way to represent privately defined characters. A database column that stores Unicode can store text written in any language.
Oracle Database users who implement globalized applications have a strong need to store Unicode data in Oracle Databases. They require a data type that is guaranteed to be Unicode regardless of the database character set.
Oracle Database supports a reliable Unicode data type through
NCHAR
,NVARCHAR2
andNCLOB
. These data types are guaranteed for Unicode encoding and always use character-length semantics. The character sets used byNCHAR
/NVARCHAR2
can beUTF8
orAL16UTF16
, depending on the national character set setting when the database is created. These data types allow Unicode character data to be stored in a database that may or may not use Unicode as its database character set.Implicit type conversion
In addition to all the implicit conversions for
CHAR
/VARCHAR2
, the Oracle Database also supports implicit conversion forNCHAR
/ NVARCHAR2. Implicit conversion betweenCHAR
/VARCHAR2
andNCHAR
/ is also supportedNVARCHAR2
.In a very concrete way.
the maximum length you want. As far as I know, Nvarchar2 is only used in Oracle. One of the features allows you a greater number of special characters.
char is a variable length data type. This is of ANSI data type. Values shorter than the column size are not well padded with the column size. Is worth to say
( "_" be considered as a space).
In this case, those 5 spaces are always saved.
nvarchar This in turn limits the maximum size of a VARCHAR to 8,000 bytes. NVARCHAR means "almost" the same, with the difference that you can store unicode data, that's where the N (uNicode) comes from.
varchar2 and varchar stores variable-length character strings determined by the (required). Its range is from 1 to 4000 characters. Is worth to say:
I hope this is clearer for you.
NOTE:
Consider that the different database engines have certain variations, but in essence it works this way.
At the moment, varchar and varchar2 are synonyms. VARCHAR2 is reserved by Oracle to support distinction between NULL and empty string, without being totally sure version 12c would already be included. VARCHAR does not distinguish between a NULL and an empty string, and never will.
If it depends on empty string and NULL is the same thing, VARCHAR2 should be used.