Reading history

SQL Data Types: INTEGER, DECIMAL, VARCHAR, DATE and CAST

5 min read 2 views

Learn the main SQL data types: integers, DECIMAL vs FLOAT, CHAR vs VARCHAR vs TEXT, dates and booleans, how SQLite stores them, and converting values with CAST.

Every column in a table has a data type that says what kind of value it holds: whole numbers, decimals, text, dates or true/false. Choosing the right type keeps data valid, makes sorting and maths behave correctly, and saves space. This lesson covers the main SQL data types, how they differ between database systems, and how to convert between them with CAST.

In this lesson you will learn

  • Numeric, text, date and boolean types
  • INTEGER vs DECIMAL vs REAL
  • CHAR vs VARCHAR vs TEXT
  • How SQLite stores types, checked with typeof()
  • Converting values with CAST

You should already know how to create a table with constraints.

Declaring Column Types

-- A table using the common SQL data types
CREATE TABLE products (
    id         INTEGER PRIMARY KEY,
    name       VARCHAR(50) NOT NULL,
    price      DECIMAL(8, 2),
    in_stock   BOOLEAN,
    added_on   DATE,
    weight_kg  REAL
);
INSERT INTO products VALUES
 (1, 'Notebook', 85.50, TRUE, '2026-03-01', 0.25),
 (2, 'Calculator', 1250, FALSE, '2026-03-15', 0.18);

-- typeof() shows how SQLite actually stored each value
SELECT name, price, typeof(price) AS price_type,
       in_stock, typeof(in_stock) AS bool_type,
       added_on, typeof(added_on) AS date_type
FROM products;

Output

+------------+-------+------------+----------+-----------+------------+-----------+
| name       | price | price_type | in_stock | bool_type | added_on   | date_type |
+------------+-------+------------+----------+-----------+------------+-----------+
| Notebook   | 85.5  | real       | 1        | integer   | 2026-03-01 | text      |
| Calculator | 1250  | integer    | 0        | integer   | 2026-03-15 | text      |
+------------+-------+------------+----------+-----------+------------+-----------+
(2 rows)
  • INTEGER holds whole numbers such as ids, counts and quantities.
  • VARCHAR(50) holds text up to 50 characters. MySQL, PostgreSQL and SQL Server enforce the limit; SQLite accepts the declaration but does not enforce the length.
  • DECIMAL(8, 2) means up to 8 digits in total, 2 of them after the decimal point, so the largest value is 999999.99. In most databases it is stored exactly, which is why it is the right choice for money.
  • BOOLEAN stores true or false. SQLite has no separate boolean type and stores TRUE as 1 and FALSE as 0, as the output shows.
  • DATE holds a calendar date. SQLite stores it as ISO text ('2026-03-01'), which still sorts correctly; other databases have true date types.
  • typeof() reveals that SQLite chose the storage class per value: 85.5 became real but 1250 stayed an integer. SQLite is flexible here; most other databases convert every value to the column’s declared type.

Converting with CAST

-- Converting between types with CAST
SELECT CAST('42' AS INTEGER) + 8   AS text_to_int,
       CAST(3.99 AS INTEGER)       AS real_to_int,
       CAST(17 AS TEXT) || ' kg'   AS int_to_text,
       CAST(7 AS REAL) / 2         AS int_to_real;

-- Comparing text with numbers sorts differently
SELECT v AS as_text FROM (SELECT '10' AS v UNION ALL SELECT '9' UNION ALL SELECT '100') AS t
ORDER BY v;

SELECT CAST(v AS INTEGER) AS as_number FROM (SELECT '10' AS v UNION ALL SELECT '9' UNION ALL SELECT '100') AS t
ORDER BY as_number;

-- Floating-point (REAL) values are approximations
SELECT 0.1 + 0.2 = 0.3 AS real_equal,
       ROUND(0.1 + 0.2, 2) = 0.3 AS rounded_equal;

Output

+-------------+-------------+-------------+-------------+
| text_to_int | real_to_int | int_to_text | int_to_real |
+-------------+-------------+-------------+-------------+
| 50          | 3           | 17 kg       | 3.5         |
+-------------+-------------+-------------+-------------+
(1 row)

+---------+
| as_text |
+---------+
| 10      |
| 100     |
| 9       |
+---------+
(3 rows)

+-----------+
| as_number |
+-----------+
| 9         |
| 10        |
| 100       |
+-----------+
(3 rows)

+------------+---------------+
| real_equal | rounded_equal |
+------------+---------------+
| 0          | 1             |
+------------+---------------+
(1 row)
  • CAST(value AS type) converts a value. Text '42' becomes the number 42; 3.99 cast to INTEGER becomes 3 because SQLite cuts off the fraction. SQL Server does the same, but PostgreSQL and MySQL round instead and would give 4.
  • Text sorts character by character, so '100' comes before '9' (the character 1 is less than 9). Cast to a number to sort numerically. This is a common bug when numbers are stored in text columns.
  • 0.1 + 0.2 = 0.3 is false because REAL (floating-point) values are binary approximations. Rounding makes the comparison work, but exact DECIMAL columns avoid the problem.

Common Data Types

CategoryTypesUse for
Whole numbersSMALLINT, INT/INTEGER, BIGINTIds, counts, quantities
Exact decimalsDECIMAL(p, s), NUMERIC(p, s)Money, measurements that must be exact
Approximate decimalsREAL, FLOAT, DOUBLE PRECISIONScientific values, sensor readings
Fixed-length textCHAR(n)Codes that are always n characters, such as 'BD'
Variable-length textVARCHAR(n), TEXTNames, emails, descriptions
Date and timeDATE, TIME, TIMESTAMP/DATETIMEBirthdays, order times
True/falseBOOLEAN (BIT in SQL Server)Flags such as is_active
BinaryBLOB, BYTEA, VARBINARYFiles or images (usually better stored outside the database)

CHAR(n) pads short values with spaces to exactly n characters, while VARCHAR(n) stores only the characters given. For most text, VARCHAR or TEXT is the better choice.

Common Mistakes

  1. Storing numbers or dates as text, which breaks sorting, comparisons and maths.
  2. Using FLOAT for money instead of DECIMAL or whole paisa in an INTEGER.
  3. Making VARCHAR limits too small, for example 20 characters for email addresses.
  4. Storing phone numbers as integers, which loses the leading zero of 01711…. Phone numbers are identifiers, not quantities, so use text.

Practice Exercises

Exercise 1. Choose types for an employees table with name, national ID number, joining date, monthly salary and whether the employee is full-time.

Show answer
CREATE TABLE employees (
    id          INTEGER PRIMARY KEY,
    name        VARCHAR(100) NOT NULL,
    nid         VARCHAR(17) UNIQUE,
    joined_on   DATE,
    salary      DECIMAL(10, 2),
    full_time   BOOLEAN DEFAULT TRUE
);

The national ID is text because it is an identifier that is never used in arithmetic.

Exercise 2. What does SELECT CAST(9.99 AS INTEGER) + CAST('1' AS INTEGER); return?

Show answer

In SQLite and SQL Server it returns 10: the cast of 9.99 cuts off the fraction to give 9, and '1' becomes the number 1. PostgreSQL rounds 9.99 to 10 and returns 11.

Frequently Asked Questions

Should I use VARCHAR or TEXT?

In PostgreSQL and SQLite they perform the same, so use VARCHAR(n) when you want the database to enforce a maximum length and TEXT otherwise. In MySQL, VARCHAR is preferred for short, indexed columns.

Why does SQLite accept a string in an INTEGER column?

SQLite uses type affinity: it tries to convert the value to the column’s type and keeps it as text if it cannot. Declare the table with STRICT (SQLite 3.37 and later) to make it reject values of the wrong type.

Summary

  • Pick the type that matches the data: integers, exact decimals, text, dates, booleans.
  • Use DECIMAL for money; REAL/FLOAT values are approximate.
  • Text sorts character by character, so keep numbers in numeric columns.
  • CAST(value AS type) converts values, but rounding rules for integers differ between databases.

Next: SQL Foreign Keys and ON DELETE CASCADE.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *