TExcelTAD
Home › SQL › SQL Data Types

SQL Data Types

Every column has a data type that says what kind of values it holds: whole numbers, decimals, text, dates, binary data. Data types differ more between databases than almost anything else in SQL.

SQLite: storage classes and type affinity

SQLite is flexible. Each value has one of five storage classes:

Storage class Holds
NULL the missing value
INTEGER signed whole numbers up to 8 bytes
REAL 8-byte floating-point numbers
TEXT strings (UTF-8)
BLOB raw bytes, stored exactly as given

A column's declared type only gives it an affinity — a preference. SQLite converts values to that type when it can. typeof() shows what was actually stored:

sql · try it yourself
CREATE TABLE demo (i INTEGER, r REAL, t TEXT, anything);

INSERT INTO demo VALUES (42, 42, 42, 42);
INSERT INTO demo VALUES ('17', '2.5', 3.0, 'hello');
INSERT INTO demo VALUES ('abc', NULL, NULL, X'CAFE');

SELECT i, typeof(i) AS ti, r, typeof(r) AS tr, t, typeof(t) AS tt, typeof(anything) AS ta
FROM demo;
1 row affected.

1 row affected.

1 row affected.

i   | ti      | r    | tr   | t    | tt   | ta
----+---------+------+------+------+------+--------
42  | integer | 42   | real | 42   | text | integer
17  | integer | 2.5  | real | 3.0  | text | text
abc | text    | NULL | null | NULL | null | blob
(3 rows)

The text '17' became the integer 17 in the INTEGER column, but 'abc' could not be converted and was stored as text. Most databases would reject 'abc' in an integer column with an error.

Tip: Since SQLite 3.37 you can declare CREATE TABLE … STRICT. Strict tables enforce types like other databases do.

sql · try it yourself
CREATE TABLE prices (product TEXT NOT NULL, price REAL NOT NULL) STRICT;
INSERT INTO prices VALUES ('Green Tea', 8.75);
INSERT INTO prices VALUES ('Mystery', 'expensive');
1 row affected.

Error: cannot store TEXT value in REAL column prices.price

Types in TAD Market

sql · try it yourself
SELECT name, type FROM pragma_table_info('orders');

SELECT typeof(order_id), typeof(order_date), typeof(shipped_date)
FROM orders WHERE order_id = 1;
name         | type
-------------+--------
order_id     | INTEGER
customer_id  | INTEGER
employee_id  | INTEGER
order_date   | TEXT
shipped_date | TEXT
status       | TEXT
(6 rows)

typeof(order_id) | typeof(order_date) | typeof(shipped_date)
-----------------+--------------------+---------------------
integer          | text               | null
(1 row)

Dates are plain TEXT, and the unshipped order's shipped_date is null.

Common types across databases

Purpose Standard / PostgreSQL MySQL SQL Server SQLite
Whole number INTEGER, BIGINT, SMALLINT INT, BIGINT, TINYINT INT, BIGINT, TINYINT INTEGER
Exact decimal (money) NUMERIC(10,2), DECIMAL(10,2) DECIMAL(10,2) DECIMAL(10,2), MONEY REAL or integer cents
Floating point REAL, DOUBLE PRECISION FLOAT, DOUBLE FLOAT, REAL REAL
Fixed-length text CHAR(n) CHAR(n) CHAR(n), NCHAR(n) TEXT
Variable text VARCHAR(n), TEXT VARCHAR(n), TEXT VARCHAR(n), NVARCHAR(n) TEXT
True / false BOOLEAN BOOLEAN (= TINYINT(1)) BIT INTEGER 0/1
Date DATE DATE DATE TEXT
Date + time TIMESTAMP DATETIME, TIMESTAMP DATETIME2 TEXT
Binary BYTEA BLOB, VARBINARY VARBINARY(MAX) BLOB
JSON JSON, JSONB JSON NVARCHAR + JSON functions TEXT + JSON functions
Unique id UUID CHAR(36)/BINARY(16) UNIQUEIDENTIFIER TEXT / BLOB

Converting types with CAST

CAST(value AS type) converts explicitly:

sql · try it yourself
SELECT CAST('42' AS INTEGER) + 1     AS int_math,
       CAST(7 AS REAL) / 2           AS real_div,
       CAST(19.99 AS INTEGER)        AS truncated,
       CAST(price AS TEXT) || ' EUR' AS label
FROM products WHERE product_id = 1;
int_math | real_div | truncated | label
---------+----------+-----------+---------
43       | 3.5      | 19        | 24.5 EUR
(1 row)

Why floating point is risky for money

REAL stores binary fractions, which cannot represent most decimal amounts exactly:

sql · try it yourself
SELECT 0.1 + 0.2 AS float_sum,
       0.1 + 0.2 = 0.3 AS equal,
       (10 + 20) / 100.0 AS from_cents;
float_sum           | equal | from_cents
--------------------+-------+-----------
0.30000000000000004 | 0     | 0.3
(1 row)

Store money as DECIMAL (where available) or as integer cents, and round when displaying.

Test yourself

What does typeof('17') stored in a SQLite INTEGER column return?

  1. text
  2. integer
  3. real
  4. null

Which type is best for exact money amounts in PostgreSQL or MySQL?

  1. FLOAT
  2. REAL
  3. DECIMAL(10,2)
  4. VARCHAR(10)

How does SQLite store dates in TAD Market?

  1. DATE type
  2. TIMESTAMP type
  3. ISO text strings
  4. Unix integers