【初心者向け】MariaDBで扱う日付と時刻リテラル:書き方・使い方をわかりやすく解説


リテラルの種類

MariaDBで利用可能な日付と時刻リテラルは以下の通りです。

1 日付リテラル

  • DDD: 年のうちの日数を3桁で指定します。
    • 例: 191 (2024年7月10日は年の191日目)
  • YY-MM-DD: 年を2桁で、月と日をハイフンで区切って指定します。
    • 例: 24-07-10
  • YYYY-MM-DD: 年、月、日をハイフンで区切って指定します。最も一般的な形式です。
    • 例: 2024-07-10

2 時間リテラル

  • HH:MM: 時と分をコロンで区切って指定します。
    • 例: 13:12
  • HH:MM:SS: 時、分、秒をコロンで区切って指定します。
    • 例: 13:12:00

3 複合リテラル

  • DDD HH:MM:SS: 年のうちの日数と時刻を組み合わせて指定します。
    • 例: 191 13:12:00
  • YYYY-MM-DD HH:MM:SS: 日付と時刻を組み合わせて指定します。
    • 例: 2024-07-10 13:12:00
  • CURRENT_TIMESTAMP: 現在の日付と時刻を表します。
    • 例: SELECT CURRENT_TIMESTAMP;
  • CURRENT_TIME: 現在時刻を表します。
    • 例: SELECT CURRENT_TIME;
  • CURRENT_DATE: 現在の日付を表します。
    • 例: SELECT CURRENT_DATE;

リテラルの使用方法

日付と時刻リテラルは、様々なSQLステートメントで使用することができます。以下に、いくつかの例を紹介します。

1 INSERTステートメント

INSERT INTO customers (name, dob)
VALUES ('John Doe', '1985-01-01');

2 SELECTステートメント

SELECT order_date, total_amount
FROM orders
WHERE order_date >= '2024-07-01';

3 UPDATEステートメント

UPDATE flights
SET departure_time = '10:00:00'
WHERE flight_id = 123;

4 WHERE句

SELECT *
FROM employees
WHERE hire_date BETWEEN '2020-01-01' AND '2023-12-31';

5 関数への引数

SELECT DATEDIFF('2024-07-10', '2020-01-01');

MariaDBでは、日付と時刻の書式設定を様々な方法で行うことができます。以下に、いくつかの例を紹介します。

1 DATE_FORMAT関数

SELECT DATE_FORMAT('2024-07-10', '%Y-%m-%d %H:%i:%S');

2 STRFTIME関数

SELECT STRFTIME('%d-%b-%Y %H:%M:%S', '2024-07-10');

3 FORMAT関数

SELECT FORMAT(CURRENT_TIMESTAMP, '%Y-%m-%d %H:%i:%S');

MariaDBにおける日付と時刻リテラルの詳細については、以下の公式ドキュメントを参照してください。



CREATE TABLE orders (
  order_id INT PRIMARY KEY AUTO_INCREMENT,
  order_date DATE NOT NULL,
  order_time TIME NOT NULL,
  total_amount DECIMAL(10,2) NOT NULL
);

INSERT INTO orders (order_date, order_time, total_amount)
VALUES (
  '2024-07-10', '13:12:00', 123.45
),
(
  '2024-07-11', '10:00:00', 250.00
),
(
  '2024-07-12', '15:30:00', 78.90
);

日付と時刻リテラルの比較

SELECT order_id, order_date, order_time
FROM orders
WHERE order_date >= '2024-07-10' AND order_date <= '2024-07-12'
AND order_time >= '10:00:00' AND order_time <= '15:30:00';

日付と時刻リテラルの計算

SELECT order_id, order_date, order_time, DATEDIFF(order_date, '2020-01-01') AS days_since_2020
FROM orders;

日付と時刻リテラルの書式設定

SELECT order_id, DATE_FORMAT(order_date, '%Y年%m月%d日') AS formatted_date,
       DATE_FORMAT(order_time, '%H:%i:%S') AS formatted_time
FROM orders;
SELECT order_id, MONTHNAME(order_date) AS month_name
FROM orders;
  • 実際のコードは、使用している MariaDB のバージョンや要件に合わせて調整する必要があります。
  • 上記のコードは、MySQL 8.0以降で使用できます。


Unix Timestamps

Unix timestamps represent dates and times as seconds since the Unix epoch, which is January 1, 1970, 00:00:00 UTC. They are a compact and widely used format for storing and exchanging date and time information.

Example

SELECT order_id, order_date, UNIX_TIMESTAMP(order_date) AS unix_timestamp
FROM orders;

ISO 8601 Format

ISO 8601 is an international standard for representing dates and times. It offers a variety of formats, including both date-only and time-only formats, as well as combined date and time formats.

Example

SELECT order_id, DATE_FORMAT(order_date, '%Y-%m-%d') AS iso_date,
       DATE_FORMAT(order_time, '%H:%M:%S') AS iso_time
FROM orders;

MySQL Specific Functions

MariaDB provides several built-in functions for working with dates and times. These functions can be used to perform various operations, such as extracting date components, calculating date differences, and converting between different date and time formats.

Example

SELECT order_id, YEAR(order_date) AS order_year,
       MONTH(order_date) AS order_month,
       DAY(order_date) AS order_day
FROM orders;

Third-Party Libraries

There are also several third-party libraries available for working with dates and times in MariaDB. These libraries can provide additional functionality and flexibility beyond what is available in the standard MariaDB library.

Example

SELECT order_id, order_date, PERIOD_DIFF(DAY, order_date, CURRENT_DATE) AS days_since_order
FROM orders;
  • MySQL specific functions and third-party libraries can provide additional functionality and flexibility.
  • ISO 8601 is a standardized format that is well-suited for international use.
  • Unix timestamps are efficient for storing and exchanging date and time information, but they may not be as user-friendly for display purposes.
  • The choice of date and time representation depends on the specific requirements of the application.