Treating date-and-time as Numbers : DAYNAME « Date Time « SQL / MySQL






Treating date-and-time as Numbers

      
mysql>
mysql> CREATE TABLE date_val
    -> (
    ->  d       DATE
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> INSERT INTO date_val (d) VALUES('1864-02-28');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO date_val (d) VALUES('1900-01-15');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO date_val (d) VALUES('1987-03-05');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO date_val (d) VALUES('1999-12-31');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO date_val (d) VALUES('2000-06-04');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql>
mysql> CREATE TABLE datetime_val
    -> (
    ->  dt      DATETIME
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql>
mysql> INSERT INTO datetime_val (dt) VALUES('1970-01-01 00:00:00');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO datetime_val (dt) VALUES('1987-03-05 12:30:15');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO datetime_val (dt) VALUES('1999-12-31 09:00:00');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO datetime_val (dt) VALUES('2000-06-04 15:45:30');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT * FROM datetime_val;
+---------------------+
| dt                  |
+---------------------+
| 1970-01-01 00:00:00 |
| 1987-03-05 12:30:15 |
| 1999-12-31 09:00:00 |
| 2000-06-04 15:45:30 |
+---------------------+
4 rows in set (0.00 sec)

mysql>
mysql> SELECT d, d+0 FROM date_val;
+------------+----------+
| d          | d+0      |
+------------+----------+
| 1864-02-28 | 18640228 |
| 1900-01-15 | 19000115 |
| 1987-03-05 | 19870305 |
| 1999-12-31 | 19991231 |
| 2000-06-04 | 20000604 |
+------------+----------+
5 rows in set (0.00 sec)

mysql> SELECT dt, dt+0 FROM datetime_val;
+---------------------+-----------------------+
| dt                  | dt+0                  |
+---------------------+-----------------------+
| 1970-01-01 00:00:00 | 19700101000000.000000 |
| 1987-03-05 12:30:15 | 19870305123015.000000 |
| 1999-12-31 09:00:00 | 19991231090000.000000 |
| 2000-06-04 15:45:30 | 20000604154530.000000 |
+---------------------+-----------------------+
4 rows in set (0.00 sec)

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

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

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.Return the name of the day: DAYNAME()
2.DAYNAME( ) is useful in conjunction with other date-related techniques.
3.Finding the Day of the Week for a Date
4.DAYNAME( ) returns the complete day name.
5.Get the name of the day
6.DayName returns Monday, Tuesday ...
7.Use DAYNAME( ) to display weekday names instead.