SQL
database
datetime
SQL insert
SQL tutorial

how to insert datetime into the SQL Database table?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Inserting a datetime value is easy once the table uses the right column type and the query passes a value in a format the database understands. Most problems come from mixing date formats, ignoring time zones, or building SQL strings manually instead of using parameters.

Choose a Column Type That Matches the Data

Start by confirming what the column is supposed to represent. A birthday often belongs in a DATE column. An event that happened at a specific moment usually belongs in DATETIME or TIMESTAMP.

Here is a simple table for application events:

sql
1CREATE TABLE app_events (
2    id INT PRIMARY KEY,
3    event_name VARCHAR(100) NOT NULL,
4    occurred_at DATETIME NOT NULL
5);

Choosing the type first matters because the correct insert syntax depends on what the column stores.

Insert an Explicit Datetime Value

If you already know the value, use an ISO-style literal. That format is widely understood and much safer than locale-specific strings such as 03/07/2026 2:30 PM.

sql
INSERT INTO app_events (id, event_name, occurred_at)
VALUES (1, 'user_login', '2026-03-07 14:30:00');

If your database supports fractional seconds and your column definition allows them, you can include them as well:

sql
INSERT INTO app_events (id, event_name, occurred_at)
VALUES (2, 'payment_captured', '2026-03-07 14:30:00.250');

This is the basic pattern for writing a known date and time directly into a SQL table.

Insert the Current Time From the Database

If the row should record "now", use the database server clock instead of generating the timestamp in application code.

sql
INSERT INTO app_events (id, event_name, occurred_at)
VALUES (3, 'cache_refresh', CURRENT_TIMESTAMP);

Using the database clock helps keep inserted values consistent when several application servers are writing rows at the same time.

Prefer Parameters in Application Code

In real applications, the safest approach is to send a native datetime object as a query parameter and let the database driver handle conversion.

python
1from datetime import datetime
2import sqlite3
3
4conn = sqlite3.connect("example.db")
5cur = conn.cursor()
6
7cur.execute(
8    """
9    INSERT INTO app_events (id, event_name, occurred_at)
10    VALUES (?, ?, ?)
11    """,
12    (4, "report_generated", datetime(2026, 3, 7, 15, 45, 0)),
13)
14
15conn.commit()
16conn.close()

This is safer than concatenating strings because it avoids SQL injection and reduces formatting mistakes.

Time Zone Rules Matter

A datetime insert is only technically correct if the system agrees on what the time means. If one service inserts local time and another inserts UTC into the same column, the data quickly becomes inconsistent.

A common practice is:

  • Store UTC in the database
  • Convert to the user's local time only when displaying data

The exact implementation depends on the database and driver, but the rule should be consistent across the application.

Even a perfect INSERT statement cannot fix inconsistent time semantics across services, so define that rule early.

Common Pitfalls

  • Locale-specific datetime strings are unreliable across databases and environments.
  • Using the wrong column type causes confusion, such as inserting a date-only value into a timestamp column.
  • Building SQL strings manually is error-prone and unsafe compared with parameterized queries.
  • If you do not define a time-zone policy, inserted datetime values from different services may not be comparable.

Summary

  • Use a column type that matches the kind of time data you need to store.
  • Insert explicit values with an ISO-style datetime string when writing SQL directly.
  • Use CURRENT_TIMESTAMP when the database should record the current moment.
  • In application code, prefer parameterized inserts so the driver handles datetime conversion safely.

Course illustration
Course illustration

All Rights Reserved.