sqlite
  1. sqlite-strftime

SQLite Date & Time: strftime()

SQLite provides a variety of date and time functions that allow you to work with date and time values in various formats. One of these functions is strftime(), which allows you to format date and time values according to a specified format code.

Syntax

The basic syntax of the strftime() function is as follows:

strftime(format, time_string, modifier, modifier, ...)

The parameters of the strftime() function are:

  • format: A string that specifies the format of the output string. This parameter is required.
  • time_string: A string that contains a timestamp in one of the formats recognized by SQLite. This parameter is required.
  • modifier: An optional string that modifies the behavior of the format code.

Example

Suppose we want to retrieve the current date and time in the format of "YYYY-MM-DD HH:MM:SS". We can use the strftime() function as follows:

SELECT strftime('%Y-%m-%d %H:%M:%S', 'now');

Output

The output of the above example would be the current date and time in the format of "YYYY-MM-DD HH:MM:SS", as follows:

2022-02-21 12:30:45

Explanation

In the example above, we use the strftime() function to format the current date and time according to the specified format code of "%Y-%m-%d %H:%M:%S". The now parameter is used to specify the current date and time.

Use

The strftime() function can be used to format date and time values in various ways. It allows you to customize the output format of date and time values to suit your needs.

Important Points

  • The format code syntax of the strftime() function is similar to that of the C programming language's strftime() function.
  • The strftime() function is case-sensitive.
  • Date and time modifiers can be used to modify the behavior of the format code.
  • SQLite recognizes several different formats for date and time values, including ISO8601 timestamps and Julian day numbers.

Summary

In this tutorial, we learned about the strftime() function in SQLite and how to use it to format date and time values. We saw an example of using strftime() to retrieve the current date and time in a specific format. The strftime() function can be used in various ways to format date and time values to suit your needs. Remember to be aware of case sensitivity and to use the appropriate format codes and modifiers.

Published on: