Introduction
In Microsoft SQL Server, every column must have a data type.
๐ Data type defines:
Why Data Types Are Important?
Without proper data types:
๐ With correct data types:
โ
Fast queries
โ
Less memory usage
โ
Data accuracy
Types of Data Types in SQL Server
1๏ธโฃ Numeric Data Types
INT (Integer)
๐ Stores whole numbers
CREATE TABLE Student (
Id INT,
Age INT
);
INSERT INTO Student VALUES (1, 20);
โ Output:
๐ Use When:
No decimal values
Example: Age, Quantity
BIGINT
๐ Stores very large numbers
BIGINT
๐ Use When:
DECIMAL / NUMERIC
๐ Stores exact decimal values
Price DECIMAL(10,2)
๐ Example:
INSERT INTO Product VALUES (101, 199.99);
โ Output:
๐ Use When:
FLOAT
๐ Stores approximate decimal values
Value FLOAT
๐ Use When:
Scientific calculations
Approximate values
โ ๏ธ Note: Not accurate like DECIMAL
2๏ธโฃ Character (String) Data Types
CHAR
๐ Fixed length string
Code CHAR(5)
๐ Example:
INSERT INTO Test VALUES ('A');
โ Output:
'A ' (extra spaces added)
๐ Use When:
VARCHAR
๐ Variable length string
Name VARCHAR(50)
๐ Example:
INSERT INTO Student VALUES ('Abhay');
โ Output:
'Abhay'
๐ Use When:
โ
Saves space
NVARCHAR
๐ Stores Unicode (multiple languages)
Name NVARCHAR(50)
๐ Example:
INSERT INTO Student VALUES (N'เช
เชญเชฏ');
โ Output:
เช
เชญเชฏ
๐ Use When:
3๏ธโฃ Date and Time Data Types
DATE
๐ Stores only date
DOB DATE
โ Example:
2026-03-31
DATETIME
๐ Stores date + time
CreatedAt DATETIME
โ Example:
2026-03-31 10:30:00
DATETIME2 (Recommended)
๐ More accurate than DATETIME
๐ Use When:
TIME
๐ Stores only time
LoginTime TIME
โ Output:
10:30:00
4๏ธโฃ Boolean Data Type
BIT
๐ Stores only:
IsActive BIT
โ Example:
๐ Use When:
5๏ธโฃ Binary Data Types
VARBINARY
๐ Stores files (images, videos)
ProfileImage VARBINARY(MAX)
๐ Use When:
6๏ธโฃ Unique Identifier
UNIQUEIDENTIFIER
๐ Generates unique ID (GUID)
Id UNIQUEIDENTIFIER DEFAULT NEWID()
โ Example Output:
A1B2C3D4-5678-90AB-CDEF-1234567890AB
๐ Use When:
7๏ธโฃ Special Data Types
XML
๐ Stores XML data
Data XML
JSON (Stored in NVARCHAR)
๐ SQL Server stores JSON as string
Data NVARCHAR(MAX)
When to Use Which Data Type (Important)
Situation โ Best Data Type
Age โ INT
Salary โ DECIMAL
Name โ VARCHAR / NVARCHAR
Date of Birth โ DATE
Login Time โ TIME
Yes/No โ BIT
Image โ VARBINARY
Unique ID โ UNIQUEIDENTIFIER
Common Mistakes
โ Using VARCHAR instead of NVARCHAR for multi-language
โ Using FLOAT for money
โ Using CHAR unnecessarily (wastes space)
โ Using DATETIME instead of DATETIME2
Pro Tips (Very Important)
Always choose smallest possible data type
Use DECIMAL for money
Use NVARCHAR for international apps
Use DATETIME2 instead of DATETIME
Real-Life Example
CREATE TABLE Employee (
Id INT,
Name NVARCHAR(100),
Salary DECIMAL(10,2),
DOB DATE,
IsActive BIT
);
Conclusion
๐ Data types are the foundation of database design
If you choose wrong:
If you choose correct: