Data types#
This page covers the specifics of SQL related to data types.
Date and time#
There are several data types that allow you to describe and operate with date and time.
The following table represents datatypes that allows to operate with date and time in PostgreSQL.
Data Type |
Description |
Example |
---|---|---|
|
Stores the date (year, month, day) without time. |
|
|
Stores the time of day (hours, minutes, seconds) without time zone. |
|
|
Stores the time of day including the time zone. |
|
|
Stores both date and time without time zone. |
|
|
Stores both date and time including the time zone. |
|
|
Represents a time span or duration (days, hours, minutes, seconds). |
|
And the same for clickhouse.
Data Type |
Description |
Example |
---|---|---|
|
Represents a date without time (YYYY-MM-DD format). |
|
|
Represents a date and time (YYYY-MM-DD HH:MM:SS format). |
|
|
Represents a date and time with fractional seconds. |
|
|
Similar to |
|
|
|
|
|
Represents time (HH:MM:SS format) without date. |
|
|
Represents a time interval (e.g., days, hours). |
|
Findout more in the special page.
Json#
PostgreSQL has special types for storing JSON structures: JSON
and JSONB
. Of course, you can store JSON structures as TEXT
in the database, but using these special types provides validation of JSON syntax and allows for JSON-specific operations directly within PostgreSQL.
Find out more on this topic on specific page in postgreSQL documentation.
Consider a table with one column defined as JSONB
. The following cell demonstrates the creation and population of such a table.
--postgreSQL
DROP TABLE IF EXISTS json_example;
CREATE TABLE json_example(
json_col JSONB
);
INSERT INTO json_example (json_col)
VALUES
('{"A": 0, "B": 0}'),
('{"A": 100}'),
('{"B": -50}'),
('{"A": {"B": 3}}'),
('{"B": -30}'),
('{"B": 100}'),
('{"B" : 90, "A": -50}')
NOTICE: table "json_example" does not exist, skipping
DROP TABLE
CREATE TABLE
INSERT 0 7
Now, to show the advantage over storing as text, let’s get the values of field A from all JSONs.
--postgreSQL
SELECT json_col, json_col->'A' A_field FROM json_example;
SELECT 7
json_col | a_field |
---|---|
{'A': 0, 'B': 0} | 0 |
{'A': 100} | 100 |
{'B': -50} | |
{'A': {'B': 3}} | {'B': 3} |
{'B': -30} | |
{'B': 100} | |
{'A': -50, 'B': 90} | -50 |