Sling Academy
Home/SQLite/Practical Applications of DATE and DATETIME Functions in SQLite

Practical Applications of DATE and DATETIME Functions in SQLite

Last updated: December 08, 2024

SQLite is a popular database used in many applications due to its simplicity and ease of use. A crucial part of working with databases is managing dates and times efficiently. SQLite provides several built-in functions to work with DATE and DATETIME types, enabling developers to handle various operations from simple date manipulations to complex time-zone adjustments.

Understanding SQLite Date and Time

In SQLite, dates and times are stored as TEXT, REAL, or INTEGER. The TEXT format corresponds to the 'YYYY-MM-DD HH:MM:SS.SSS' format, REAL is for Julian dates, and INTEGER leverages Unix time (the number of seconds since 1970-01-01).

Common DATE and DATETIME Functions

SQLite provides several functions for working with dates and times:

  • date(timestring, modifier, ...) - Returns the date part in 'YYYY-MM-DD' format.
  • time(timestring, modifier, ...) - Delivers the time part in 'HH:MM:SS' format.
  • datetime(timestring, modifier, ...) - Provides 'YYYY-MM-DD HH:MM:SS' format.
  • julianday(timestring, modifier, ...) - Returns the Julian day number.
  • strftime(format, timestring, modifier, ...) - Custom date/time formatting function.

Practical Examples

Below are some practical examples using these functions:

Getting the Current Date and Time

To get the current date and time:

SELECT date('now'); -- Output format: YYYY-MM-DD
SELECT time('now'); -- Output format: HH:MM:SS
SELECT datetime('now'); -- Output format: YYYY-MM-DD HH:MM:SS

Date Manipulations

Manipulating dates involves adding or subtracting days, months, or years. Here's how you can add days to the current date:

SELECT date('now', '+7 days'); -- Adds seven days to the current date
SELECT date('now', '-1 month'); -- Subtracts one month from the current date

Calculating Age from Birthdate

To calculate age from a birthdate using the date function, you could use:

SELECT strftime('%Y', 'now') - strftime('%Y', '1990-05-01') -
       (strftime('%m-%d', 'now') < strftime('%m-%d', '1990-05-01'))
as age;

Formatting Dates

Custom formats using the strftime function can be particularly powerful:

SELECT strftime('%d-%m-%Y %H:%M:%S', 'now'); -- Custom format: DD-MM-YYYY HH:MM:SS

Handling Time-Zones

By default, SQLite operations happen in the UTC timezone. However, you can manage local or another timezone using modifiers:

SELECT datetime('now', 'localtime'); -- Current local date and time
SELECT datetime('now', 'localtime', '+2 hours'); -- Adds two hours to local time

Conclusion

SQLite date and datetime functions play an essential role in managing temporal data efficiently and effectively. Whether you need to store dates, calculate durations, or format datetime strings, these functions provide the necessary interfaces. Understanding these tools empowers developers to write both simple and complex queries, facilitating powerful database interactions in local or distributed applications.

Next Article: Simplifying Complex Queries with SQLite Mathematical Functions

Previous Article: Mastering SQLite String Functions: SUBSTR, REPLACE, and More

Series: SQLite Functions and Extensions

SQLite

You May Also Like

  • How to use regular expressions (regex) in SQLite
  • SQLite UPSERT tutorial (insert if not exist, update if exist)
  • What is the max size allowed for an SQLite database?
  • SQLite Error: Invalid Value for PRAGMA Configuration
  • SQLite Error: Failed to Load Extension Module
  • SQLite Error: Data Type Mismatch in INSERT Statement
  • SQLite Warning: Query Execution Took Longer Than Expected
  • SQLite Error: Cannot Execute VACUUM on Corrupted Database
  • SQLite Error: Missing Required Index for Query Execution
  • SQLite Error: FTS5 Extension Malfunction Detected
  • SQLite Error: R-Tree Node Size Exceeds Limit
  • SQLite Error: Session Extension: Invalid Changeset Detected
  • SQLite Error: Invalid Use of EXPLAIN Statement
  • SQLite Warning: Database Connection Not Closed Properly
  • SQLite Error: Cannot Attach a Database in Encrypted Mode
  • SQLite Error: Insufficient Privileges for Operation
  • SQLite Error: Cannot Bind Value to Parameter
  • SQLite Error: Maximum String or Blob Size Exceeded
  • SQLite Error: Circular Reference in Foreign Key Constraints