Data Type
SQL Server Data Types Explained with Examples for Beginners
Introduction
When creating a table in SQL Server, we need to decide what type of information each column will store.
For example, a student's age is a number, a student's name is text, and a registration date is a date.
SQL Server provides different data types to store these values correctly.
Choosing the right data type helps maintain accurate information, manage storage, and make database queries more efficient.
In this tutorial, we'll understand common SQL Server data types, including INT, VARCHAR, VARCHAR(MAX), NVARCHAR, CHAR, and NCHAR.
1. What Is a Data Type in SQL Server?
A data type defines the kind of value a column or variable can store.
For example:
CREATE TABLE Students
(
StudentID INT,
StudentName NVARCHAR(100),
Age INT,
City VARCHAR(50)
);Here, StudentID and Age store integers, while StudentName and City store text.
2. INT Data Type
INT is used to store whole numbers without decimal places.
Examples include:
10
25
100
-50SQL Server's INT supports values from -2,147,483,648 to 2,147,483,647.
Example:
DECLARE @Age INT = 25;
SELECT @Age AS Age;Use INT for numeric values such as quantities, ages, and identifiers when its range is appropriate.
It is not suitable for storing phone numbers because phone numbers are identifiers rather than quantities and may contain leading zeros or a plus sign.
3. VARCHAR Data Type
VARCHAR stores variable-length, non-Unicode character data.
It can store letters, digits, spaces, and supported symbols.
For example:
DECLARE @City VARCHAR(50) = 'Mumbai';
SELECT @City AS City;What Does VARCHAR(50) Mean?
VARCHAR(50) allows up to 50 bytes of character data.
For many common single-byte characters, that corresponds to 50 characters. With UTF-8 collations, some characters require multiple bytes.
Examples of values include:
Mumbai
Pune
Student123
ABC@123Use VARCHAR when its character encoding and length are suitable for your application.
4. VARCHAR(MAX) Data Type
VARCHAR(MAX) stores large amounts of variable-length, non-Unicode text.
It can hold up to approximately 2 GB of character data, subject to SQL Server limits.
Example:
CREATE TABLE Articles
(
ArticleID INT PRIMARY KEY,
Description VARCHAR(MAX)
);VARCHAR(MAX) is useful for potentially large text such as article content or long descriptions.
However, it should not automatically be used for every text column. If a reasonable maximum length is known, a bounded type such as VARCHAR(100) is often more appropriate.
5. NVARCHAR Data Type
NVARCHAR stores variable-length Unicode text.
It is useful when an application needs to support names or content in multiple languages.
For example:
DECLARE @Name NVARCHAR(100) = N'Rahul';
SELECT @Name AS StudentName;The N prefix identifies a Unicode string literal.
NVARCHAR can store letters, numbers, spaces, symbols, and characters from many writing systems.
For example:
DECLARE @Greeting NVARCHAR(100) = N'नमस्ते';
SELECT @Greeting AS Greeting;VARCHAR vs NVARCHAR
| VARCHAR | NVARCHAR |
|---|---|
| Non-Unicode character type | Unicode character type |
| Encoding depends on collation | Uses SQL Server's Unicode encoding |
| Often suitable for limited character sets | Suitable for multilingual text |
| Length is specified in bytes | Length is specified in UTF-16 byte-pairs |
For modern multilingual applications, NVARCHAR is often a convenient choice. SQL Server also supports UTF-8 collations for VARCHAR in supported versions.
6. CHAR Data Type
CHAR stores fixed-length, non-Unicode character data.
For example:
DECLARE @Code CHAR(5) = 'AB';
SELECT @Code AS Code;SQL Server pads shorter values with spaces to fit the fixed length.
CHAR is useful when values have a consistent fixed size, such as certain codes.
7. NCHAR Data Type
NCHAR stores fixed-length Unicode text.
It is not restricted to a single character.
For example:
DECLARE @Code NCHAR(5) = N'AB';
SELECT @Code AS Code;Like CHAR, NCHAR pads shorter values to the defined length.
The difference is that NCHAR supports Unicode text.
8. Difference Between CHAR, VARCHAR, NCHAR and NVARCHAR
| Data Type | Length | Character Support |
|---|---|---|
CHAR(n) | Fixed | Non-Unicode |
VARCHAR(n) | Variable | Non-Unicode |
NCHAR(n) | Fixed | Unicode |
NVARCHAR(n) | Variable | Unicode |
For NCHAR(n) and NVARCHAR(n), the length parameter counts UTF-16 byte-pairs, not necessarily individual visible characters.
For example, some supplementary Unicode characters use two UTF-16 byte-pairs.
9. Other Common SQL Server Data Types
SQL Server provides many additional data types.
| Data Type | Purpose | Example |
|---|---|---|
BIGINT | Large whole numbers | 9000000000 |
BIT | Boolean-like values | 0 or 1 |
DECIMAL(10,2) | Exact decimal numbers | 1250.75 |
FLOAT | Approximate floating-point numbers | 3.14159 |
DATE | Calendar dates | 2026-11-12 |
DATETIME2 | Date and time | 2026-11-12 14:30:00 |
UNIQUEIDENTIFIER | GUID values | A generated GUID |
VARBINARY(MAX) | Binary data | File contents |
For prices and financial calculations, DECIMAL is generally preferable to FLOAT because it stores decimal values with defined precision and scale.
10. Practical Example: Create a Student Table
Let's create a table using several data types.
CREATE TABLE dbo.Students
(
StudentID INT IDENTITY(1,1) PRIMARY KEY,
StudentName NVARCHAR(100) NOT NULL,
Age INT NULL,
Email VARCHAR(150) NULL,
City NVARCHAR(100) NULL,
AdmissionDate DATE NULL,
IsActive BIT NOT NULL DEFAULT 1
);Insert Sample Data
INSERT INTO dbo.Students
(StudentName, Age, Email, City, AdmissionDate)
VALUES
(N'Rahul', 20, 'rahul@example.com', N'Pune', '2026-11-12'),
(N'Priya', 21, 'priya@example.com', N'Mumbai', '2026-11-12');Retrieve Data
SELECT
StudentID,
StudentName,
Age,
Email,
City,
AdmissionDate,
IsActive
FROM dbo.Students;This example shows how different data types work together in a real database table.
11. Common Mistakes When Choosing Data Types
Using INT for Phone Numbers
Phone numbers may contain leading zeros, country codes, and formatting characters. Use an appropriate text type instead.
Using VARCHAR(MAX) for Every Column
This can make schema design and some database operations less efficient. Define realistic maximum lengths when possible.
Assuming NVARCHAR Cannot Store Symbols
NVARCHAR supports Unicode text, including many symbols.
Assuming NCHAR Stores Only One Character
NCHAR(n) stores fixed-length Unicode text. Its size depends on the specified n.
Using FLOAT for Exact Prices
Use DECIMAL for values that require exact decimal precision, such as prices.
12. Frequently Asked Questions
What is a data type in SQL Server?
A data type defines the kind of value that a database column or variable can store.
What is the difference between INT and VARCHAR?
INT stores whole numbers, while VARCHAR stores character data.
What does VARCHAR(50) mean?
It means the column can store up to 50 bytes of character data.
What is VARCHAR(MAX)?
It is a variable-length character data type used for large text values.
Can NVARCHAR store special characters?
Yes. NVARCHAR supports Unicode text, including many special characters and characters from different languages.
What is the difference between NCHAR and NVARCHAR?
NCHAR stores fixed-length Unicode text, while NVARCHAR stores variable-length Unicode text.
Which data type should I use for a person's name?
NVARCHAR is often a good choice when multilingual character support is needed.
Which data type is best for prices?
DECIMAL is generally recommended for prices and other values requiring exact decimal precision.
Conclusion
SQL Server data types determine how information is stored in database tables.
In this tutorial, we learned that INT stores whole numbers, VARCHAR stores variable-length character data, NVARCHAR supports Unicode text, and CHAR and NCHAR store fixed-length text.
We also explored VARCHAR(MAX) and several other useful SQL Server data types.
Understanding these differences helps you create more reliable tables and avoid common database design mistakes.
Comments
Post a Comment