Convert JSON Date Strings to SQL DATE and TIMESTAMP Columns
Learn how JSON ISO 8601 date strings map to SQL DATE, TIMESTAMP, and DATETIME column types. Covers timezone handling and precision options.
Detailed Explanation
Date and Timestamp Mapping
JSON has no native date type — dates are encoded as strings, typically in ISO 8601 format. The converter detects date-like strings and maps them to the appropriate SQL temporal type.
Example JSON
{
"id": 1,
"title": "Project Alpha",
"start_date": "2024-06-15",
"created_at": "2024-06-15T10:30:00Z",
"updated_at": "2024-06-15T10:30:00.123456Z",
"deadline": "2024-12-31T23:59:59+09:00"
}
Generated SQL
CREATE TABLE projects (
id INTEGER NOT NULL,
title VARCHAR(255) NOT NULL,
start_date DATE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP(6) WITH TIME ZONE NOT NULL,
deadline TIMESTAMP WITH TIME ZONE NOT NULL
);
Detection Heuristics
The converter identifies date strings by:
- ISO 8601 patterns —
YYYY-MM-DD,YYYY-MM-DDTHH:mm:ssZ, etc. - Column name hints — Names containing
_at,_date,_time,created,updated,deleted,expires. - Unix timestamps — Large integers (>1_000_000_000) in columns named
timestamp,created_at, etc.
SQL Type Selection
| Pattern | SQL type |
|---|---|
2024-06-15 (date only) |
DATE |
2024-06-15T10:30:00 (no tz) |
TIMESTAMP / DATETIME |
2024-06-15T10:30:00Z (with tz) |
TIMESTAMP WITH TIME ZONE |
| Fractional seconds | TIMESTAMP(6) for microsecond precision |
Timezone Best Practice
Always store timestamps with timezone information. Use TIMESTAMP WITH TIME ZONE (PostgreSQL TIMESTAMPTZ) so that values are normalized to UTC on storage and converted to the session timezone on retrieval. Storing without timezone leads to ambiguity when your application spans multiple regions.
Auto-Generated Timestamps
For created_at and updated_at columns, the converter adds:
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
You should also add a trigger or application-level logic to update updated_at on every row modification.
Database Differences
- PostgreSQL:
TIMESTAMPTZis an alias forTIMESTAMP WITH TIME ZONE. - MySQL:
DATETIMEstores values without timezone;TIMESTAMPconverts to UTC but has a limited range (up to 2038). - SQL Server:
DATETIMEOFFSETstores timezone offset explicitly.
Use Case
You are importing event data from a scheduling API that returns ISO 8601 dates with mixed timezone formats, and you need each column to use the correct SQL temporal type with proper timezone handling.
Try It — JSON to SQL Schema
Related Topics
Generate a Simple CREATE TABLE from JSON
Basic Tables
Map JSON Numbers to SQL Integer and Numeric Columns
Basic Tables
Handle Nullable JSON Fields in SQL Schema Generation
Constraints
Generate SQL Primary Key Constraints from JSON
Constraints
Generate a Complete User Table Schema from JSON
Real-World Schemas