Data Types
Numeric, string and date/time data types in SQL Server — and how to choose the right one for each column.
Every column in a table can hold only one kind of data. The runs column holds numbers, the name column holds text, and a match_date column holds dates. This rule is enforced by the column's data type. Picking the right type keeps your data clean, saves storage and makes queries fast.
Definition
A data type is a rule attached to a column that defines what kind of values it can store (numbers, text, dates …), how much space each value uses, and which operations are allowed on it.
Because runs is an INT, you can add, average and compare runs. If someone tries to store the word 'fifty' in it, SQL Server refuses. That is the database protecting data quality for you.
Numeric types
| Type | Stores | Example value | IPL use |
|---|---|---|---|
| INT | Whole numbers (about ±2.1 billion) | 750 | runs, wickets, id |
| BIGINT | Very large whole numbers | 30000000 | ball-by-ball row ids |
| SMALLINT / TINYINT | Small whole numbers (TINYINT = 0–255) | 6 | sixes in an over |
| DECIMAL(p, s) | Exact decimals — p total digits, s after the point | 139.50 | strike rate, ticket price |
| FLOAT | Approximate decimals (scientific) | 0.1234567 | ML scores — not money! |
| BIT | 0 or 1 (true/false) | 1 | is_captain |
Tip:
DECIMAL(5, 2)can store up to 999.99 — 5 digits in total, 2 of them after the decimal point. Always use DECIMAL (not FLOAT) for money, because FLOAT can introduce tiny rounding errors.
String (text) types
| Type | Stores | Example |
|---|---|---|
| CHAR(n) | Fixed-length text — always uses n characters (pads with spaces) | 'RCB' in CHAR(3) |
| VARCHAR(n) | Variable-length text up to n characters — uses only what it needs | 'Virat Kohli' in VARCHAR(50) |
| NVARCHAR(n) | Like VARCHAR but Unicode — supports Hindi, Tamil, emojis … | N'विराट कोहली' |
Use CHAR when every value has the same length (team codes like RCB, CSK), and VARCHAR when length varies (player names). In SQL, text values are always written inside single quotes: 'India'.
Date & time types
| Type | Stores | Example |
|---|---|---|
| DATE | Date only | '2025-03-22' |
| TIME | Time only | '19:30:00' |
| DATETIME | Date + time (older, accurate to ~3 ms) | '2025-03-22 19:30:00' |
| DATETIME2 | Date + time, more precise (newer, recommended) | '2025-03-22 19:30:00.0000000' |
Note: Write dates in the
'YYYY-MM-DD'format. It is unambiguous everywhere —'03/04/2025'could mean 3 April or 4 March depending on settings.
Example — a table with every kind of data type
sql
CREATE TABLE matches (
match_id INT PRIMARY KEY,
home_team CHAR(3),
venue VARCHAR(60),
ticket_price DECIMAL(8, 2),
match_date DATE,
start_time TIME,
is_final BIT
);How it works: each column name is followed by its data type. match_id must be a whole number, home_team is always 3 characters, venue can be up to 60 characters, ticket_price is an exact money value like 1499.00, and is_final is a 0/1 flag.
Choosing the right data type
- Numbers you calculate with → INT / DECIMAL (never store runs as text).
- Money → DECIMAL, not FLOAT.
- Codes of fixed length → CHAR(n); names and descriptions → VARCHAR(n) / NVARCHAR(n).
- Dates → DATE / DATETIME2, never VARCHAR — otherwise you can't sort or do date maths correctly.
- Phone numbers and PIN codes → VARCHAR (you never add them, and leading zeros must be kept).
Common mistakes
- ❌ Storing dates as text (
'22-03-2025'). ✅ Use DATE so sorting and date functions work. - ❌
VARCHARwith no length in a declaration — in SQL Server this means 1 character! ✅ Always give a length:VARCHAR(50). - ❌ Writing text without quotes:
WHERE country = India. ✅WHERE country = 'India'.
SQL Server vs MySQL / PostgreSQL
| Idea | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| True/false | BIT | BOOLEAN (TINYINT) | BOOLEAN |
| Date + time | DATETIME2 | DATETIME | TIMESTAMP |
| Unicode text | NVARCHAR | VARCHAR (utf8mb4) | VARCHAR / TEXT |
Note: The browser playground uses SQLite underneath, which is relaxed about data types. Real SQL Server is strict — always design columns as if the database will enforce them.
Interview tip
Q: What is the difference between CHAR and VARCHAR?
A: CHAR(n) is fixed-length — it always uses n characters and pads shorter values with spaces, which suits fixed-size codes. VARCHAR(n) is variable-length — it stores only the characters you give it (plus a small overhead), which saves space for values like names.
Key points to remember
- A data type defines what kind of values a column can store, how much space it uses and which operations are allowed.
- Numeric: INT for whole numbers, DECIMAL(p, s) for exact decimals such as money, FLOAT for approximate values.
- String: CHAR(n) is fixed-length, VARCHAR(n) is variable-length, NVARCHAR(n) supports Unicode (Hindi, emojis).
- Date & time: DATE, TIME, DATETIME and the more precise DATETIME2.
- Text and date values are written in single quotes; use the 'YYYY-MM-DD' date format.
- Never store numbers or dates as text — you lose correct sorting and calculations.
Try it yourself
Create a table with different data types
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Data types in the IPL tables
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: design your own table
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
