dates and times in SQL commands must be given as character strings, : TimeStamp « Data Type « SQL / MySQL






dates and times in SQL commands must be given as character strings,

      
mysql>
mysql>
The following three queries are equivalent, and work for both DATETIME and TIMESTAMP columns:
mysql>
mysql> CREATE TABLE mytable
    -> (
    ->  ts      TIMESTAMP NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 10 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 9 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 8 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 7 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 6 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 5 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 4 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 3 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 2 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 1 DAY);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mytable(ts) VALUES (NOW() - INTERVAL 0 DAY);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT COUNT(*) FROM mytable WHERE ts BETWEEN '2004-05-16 08:34:07' AND '2005-02-11 00:15:44';
+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> SELECT COUNT(*) FROM mytable WHERE ts BETWEEN '2004/05/16 08:34:07' AND '2005/02/11 00:15:44';
+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> SELECT COUNT(*) FROM mytable WHERE ts BETWEEN '20110516083407' AND '20110211001544';
+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

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

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.TIMESTAMP as column type
2.timestamp type column default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
3.Table definition includes a YEAR column and a TIMESTAMP
4.Order by timestamp value
5.Recording a Row's Last Modification Time
6.Recording a Row's Creation Time
7.Performing Calculations with TIMESTAMP Values
8.Updates to tsdemo2 records that don't actually modify a column cause no change to TIMESTAMP values
9.Create a table in which timestamps can be stored.
10.The difference between the creation and modification times
11.Issuing an UPDATE statement that doesn't actually change the values in the val column doesn't update the TIMES
12.microseconds component is removed.