Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Character strings
Unicode character strings
1 de 5 4/7/2010 1:48 PM
Character string data types http://searchsqlserver.techtarget.com/generic/0,295582,sid87_gci122849...
NOTE
In some cases, you can use different identifiers to refer to a data type in T-SQL code. For example, the char data type can be
referenced as character, and varchar can be referenced as character varying. Some of these synonyms are based on ANSI SQL-92
standard requirements.
Character Strings
Character data types store character strings. The four different character types vary in length and storage characteristics:
char
varchar
varchar(max)
text
The char data type is used to store strings of fixed size. As noted earlier, the maximum size of this data type is 8,000 characters,
which is a significant increase over the 255-character limit in early versions. When a variable or a table column is assigned with a
string that is shorter than its nominal size, it is padded with trailing spaces to fill the specified field length.
The varchar data type stores strings of variable size up to 8,000 characters long. When a character value whose length is less than
the nominal size is assigned to the column or variable, SQL Server does not add trailing spaces to it, but records it as is. varchar
data types occupy two additional bytes in order to record the length of the string.
NOTE
Maintenance of this information requires some additional computation during I/O operation, but that time is usually countered by
savings in the space required. A record using such columns occupies less space, and more records fit into a single page. Therefore,
SQL Server reads more records when accessing data, and it is more likely that a single page contains the information that the user is
looking for.
The text data type is used to store huge amounts of data. One field can store up to 2GB (231 – 1 bytes) of information. Only a
16-byte pointer to this data is stored in the table. Therefore, additional processing overhead is involved with the use of text columns.
There are special functions for processing text values.
NOTE
You should avoid using this data type because Microsoft plans to remove it in future versions of SQL Server. You should use the
new varchar(max) data type instead.
Varchar(max) looks and operates like the varchar data type, but it is by internal structure and functionality actually much more like
2 de 5 4/7/2010 1:48 PM
Character string data types http://searchsqlserver.techtarget.com/generic/0,295582,sid87_gci122849...
Character constants are delimited from the rest of the Transact-SQL code with quotes. For example, the following statement inserts
contact information:
nchar
nvarchar
nvarchar(max)
ntext
The main difference between these new data types and the older character data types is that the new data types can hold Unicode
characters, which occupy 2 bytes per character. Therefore, the maximum string length that they can store is half that of the
corresponding older data types (4,000 for nchar and nvarchar).
The following statement creates the same table as the previous example but uses Unicode data types:
Unicode character constants are also delimited with quotes but are prefixed with N':
This N' prefix might look a little odd, but you will get used to it. Microsoft documentation is full of samples with Unicode constants. It
was some time before I discovered the reason Microsoft uses N' as a prefix. It stands for "National." In fact, acceptable alternative
identifiers for these data types are
National char
National char varying
National char varying(max)
National text
TIP
Typically, it is not a problem if you omit the N' prefix on constants. SQL Server automatically converts the string to its Unicode
equivalent. Naturally, it is better to insert it whenever you are dealing with Unicode columns or variables, but it is not a big problem.
The CPU will just have to perform a couple of extra cycles to make the conversion.
However, there are cases in which it becomes a problem. When your string constant is part of a query criterion, then the presence of
the N' prefix might significantly affect execution of the query. If the column is defined as a non-Unicode string and the criterion is
specified with the N' prefix, SQL Server converts every row of the table to compare it with the Unicode constant. As a result, the
query performs a table scan instead of using an index.
3 de 5 4/7/2010 1:48 PM
Character string data types http://searchsqlserver.techtarget.com/generic/0,295582,sid87_gci122849...
The previous tip is from "15 tips in 15 minutes: Basic Transact-SQL Programming Constructs,"
excerpted from Chapter 3, of the book "Microsoft SQL Server 2005 Stored Procedure Programming in
T-SQL & .NET" by Dejan Sunderic, courtesy of McGraw-Hill Publishing.
RELATED CONTENT
SQL/Transact SQL (T-SQL)
Native string manipulation functions for SQL Server
Combining result sets from multiple SQL Server queries
Using DELETE and TRUNCATE TABLE statements to delete data in SQL Server
Working with IntelliSense in SQL Server 2008 Management Studio
SQL Server Mailbag: Stored procedures, triggers and SSRS reports
Working with sparse columns in SQL Server 2008
Determining the source of full transaction logs in SQL Server
New GROUP BY option provides better data control in SQL Server 2008
Using the OPENROWSET function in SQL Server
Loading data files with SQL Server's BULK INSERT statement
SQL/Transact SQL (T-SQL) Research
RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary
About Us | Contact Us | For Advertisers | For Business Partners | Site Index | RSS SEARCH
4 de 5 4/7/2010 1:48 PM
Character string data types http://searchsqlserver.techtarget.com/generic/0,295582,sid87_gci122849...
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective
purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online
magazines.
All Rights Reserved, Copyright 2005 - 2010, TechTarget | Read our Privacy Policy
5 de 5 4/7/2010 1:48 PM