SQL Server Data Types
SQL Server data types define the kind of data that can be stored in a column, variable or expression. Choosing the correct data type helps SQL Server store data efficiently and maintain data accuracy.
What are Data Types?
A data type specifies what kind of value can be stored in a database column or variable. For example, a student's name can be stored using a character data type, while a student's age can be stored using a numeric data type.
Every column in a SQL Server table has a data type. The data type determines the type of values that the column can contain.
Why are Data Types Important?
Selecting the appropriate data type is important because it affects storage, performance and the type of values that can be stored in a column.
- Helps maintain data accuracy.
- Controls the type of values stored in a column.
- Helps use database storage efficiently.
- Makes database design more organized.
How Data Types Work
When creating a table, we define a data type for every column. SQL Server then uses that definition when storing and processing the data.
Column
↓
Data Type
↓
Allowed Values
↓
Stored Data
SQL Server Data Type Categories
SQL Server provides different categories of data types for storing different kinds of information.
| Category | Used For | Examples |
|---|---|---|
| Numeric | Numbers and mathematical values | int, bigint, decimal, float |
| Character & String | Text and character data | char, varchar, nchar, nvarchar |
| Date & Time | Date and time values | date, datetime, time |
| Binary | Binary data | binary, varbinary |
| Other | Special types of data | bit, uniqueidentifier, xml |
Common SQL Server Data Types
| Data Type | Purpose | Example |
|---|---|---|
| int | Stores integer numbers. | 100 |
| varchar | Stores variable-length character data. | 'Rahul' |
| nvarchar | Stores variable-length Unicode character data. | N'Rahul' |
| decimal | Stores exact numeric values. | 12500.50 |
| date | Stores date values. | '2026-10-02' |
| bit | Stores Boolean-like values. | 0 / 1 |
Example: Using Data Types
The following example creates a Students table using different SQL Server data types.
CREATE TABLE Students
(
StudentId INT,
StudentName VARCHAR(100),
Age INT,
Fees DECIMAL(10,2),
BirthDate DATE,
IsActive BIT
);
Here, each column uses a data type suitable for the kind of information that it stores.
Choosing the Correct Data Type
The data type should be selected according to the kind of data that the column needs to store.
| Requirement | Suitable Data Type |
|---|---|
| Student ID | INT |
| Student Name | VARCHAR / NVARCHAR |
| Course Fees | DECIMAL |
| Date of Birth | DATE |
| Active / Inactive Status | BIT |
Important
Always choose a data type based on the actual data that needs to be stored. Using an inappropriate data type can lead to unnecessary storage usage or unexpected results.
Summary
In this section, we learned what SQL Server data types are, why they are important and the major categories of data types. We also looked at common data types and how to choose an appropriate data type when designing a table.