Date-Based Sorting : DATE « Date Time « SQL / MySQL

Home
SQL / MySQL
1.Aggregate Functions
2.Backup Load
3.Command MySQL
4.Cursor
5.Data Type
6.Database
7.Date Time
8.Engine
9.Event
10.Flow Control
11.FullText Search
12.Function
13.Geometric
14.I18N
15.Insert Delete Update
16.Join
17.Key
18.Math
19.Procedure Function
20.Regular Expression
21.Select Clause
22.String
23.Table Index
24.Transaction
25.Trigger
26.User Permission
27.View
28.Where Clause
29.XML
SQL / MySQL » Date Time » DATE 
Date-Based Sorting
    
mysql>
mysql> CREATE TABLE temporal_val
    -> (
    ->  d       DATE,
    ->  dt      DATETIME,
    ->  t       TIME,
    ->  ts      TIMESTAMP
    -> );
mysql>
mysql> INSERT INTO temporal_val (d, dt, t, ts)
    ->  VALUES
    ->          ('1970-01-01','1884-01-01 12:00:00','13:00:00','1980-01-01 02:00:00'),
    ->          ('1999-01-01','1860-01-01 12:00:00','19:00:00','2021-01-01 03:00:00'),
    ->          ('1981-01-01','1871-01-01 12:00:00','03:00:00','1975-01-01 04:00:00'),
    ->          ('1964-01-01','1899-01-01 12:00:00','01:00:00','1985-01-01 05:00:00')
    -> ;
Query OK, rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT FROM temporal_val;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
1970-01-01 1884-01-01 12:00:00 13:00:00 1980-01-01 02:00:00 |
1999-01-01 1860-01-01 12:00:00 19:00:00 2021-01-01 03:00:00 |
1981-01-01 1871-01-01 12:00:00 03:00:00 1975-01-01 04:00:00 |
1964-01-01 1899-01-01 12:00:00 01:00:00 1985-01-01 05:00:00 |
1970-01-01 1884-01-01 12:00:00 13:00:00 1980-01-01 02:00:00 |
1999-01-01 1860-01-01 12:00:00 19:00:00 2021-01-01 03:00:00 |
1981-01-01 1871-01-01 12:00:00 03:00:00 1975-01-01 04:00:00 |
1964-01-01 1899-01-01 12:00:00 01:00:00 1985-01-01 05:00:00 |
1970-01-01 1884-01-01 12:00:00 13:00:00 1980-01-01 02:00:00 |
1999-01-01 1860-01-01 12:00:00 19:00:00 2021-01-01 03:00:00 |
1981-01-01 1871-01-01 12:00:00 03:00:00 1975-01-01 04:00:00 |
1964-01-01 1899-01-01 12:00:00 01:00:00 1985-01-01 05:00:00 |
+------------+---------------------+----------+---------------------+
12 rows in set (0.00 sec)

mysql>
mysql> SELECT FROM temporal_val ORDER BY d;
+------------+---------------------+----------+---------------------+
| d          | dt                  | t        | ts                  |
+------------+---------------------+----------+---------------------+
1964-01-01 1899-01-01 12:00:00 01:00:00 1985-01-01 05:00:00 |
1964-01-01 1899-01-01 12:00:00 01:00:00 1985-01-01 05:00:00 |
1964-01-01 1899-01-01 12:00:00 01:00:00 1985-01-01 05:00:00 |
1970-01-01 1884-01-01 12:00:00 13:00:00 1980-01-01 02:00:00 |
1970-01-01 1884-01-01 12:00:00 13:00:00 1980-01-01 02:00:00 |
1970-01-01 1884-01-01 12:00:00 13:00:00 1980-01-01 02:00:00 |
1981-01-01 1871-01-01 12:00:00 03:00:00 1975-01-01 04:00:00 |
1981-01-01 1871-01-01 12:00:00 03:00:00 1975-01-01 04:00:00 |
1981-01-01 1871-01-01 12:00:00 03:00:00 1975-01-01 04:00:00 |
1999-01-01 1860-01-01 12:00:00 19:00:00 2021-01-01 03:00:00 |
1999-01-01 1860-01-01 12:00:00 19:00:00 2021-01-01 03:00:00 |
1999-01-01 1860-01-01 12:00:00 19:00:00 2021-01-01 03:00:00 |
+------------+---------------------+----------+---------------------+
12 rows in set (0.00 sec)

mysql>
mysql> drop table temporal_val;
Query OK, rows affected (0.00 sec)

   
    
    
    
  
Related examples in the same category
1.Extract just the date: DATE()
2.Treating Dates or Times as Numbers
3.Synthesizing Dates or Times Using Formatting Functions
4.Synthesizing Dates or Times Using Component-Extraction Functions
5.Date-Based Summaries
6.Add an incorrect date to a special table and show the result.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.