Monday, March 30, 2009

Date

mysql> SELECT YEAR('2003-03-31');
+--------------------+
YEAR('2003-03-31')
+--------------------+
2003
+--------------------+

mysql> SELECT YEAR('2003-03-31')+5;
+----------------------+
YEAR('2003-03-31')+5
+----------------------+
2008
+----------------------+

mysql> SELECT NOW();
+---------------------+
NOW()
+---------------------+
2003-03-31 00:32:21
+---------------------+

mysql> SELECT CURRENT_DATE();
+----------------+
CURRENT_DATE()
+----------------+
2003-03-31
+----------------+

mysql> SELECT MONTH(NOW()) AS m, DAYOFMONTH(NOW()) AS d, HOUR(NOW()) AS h, MINUTE(NOW()) AS m, SECOND(NOW()) AS s;
+------+------+------+------+------+
m d h m s
+------+------+------+------+------+
3 31 1 53 38
+------+------+------+------+------+

mysql> SELECT DAYNAME('2000-01-01');
+-----------------------+
DAYNAME('2000-01-01')
+-----------------------+
Saturday
+-----------------------+

mysql> SELECT DAYOFWEEK('2000-01-01');
+-------------------------+
DAYOFWEEK('2000-01-01')
+-------------------------+
7
+-------------------------+

mysql> SELECT DAYOFYEAR('2000-12-31');
+-------------------------+
DAYOFYEAR('2000-12-31')
+-------------------------+
366
+-------------------------+

mysql> select (TO_DAYS('2009-03-30')-TO_DAYS('2009-03-01'));
+-----------------------------------------------+
(TO_DAYS('2009-03-30')-TO_DAYS('2009-03-01'))
+-----------------------------------------------+
29
+-----------------------------------------------+

mysql> SELECT (TO_DAYS(CURRENT_DATE())-TO_DAYS('2009-03-01'));
+-------------------------------------------------+
(TO_DAYS(CURRENT_DATE())-TO_DAYS('2009-03-01'))
+-------------------------------------------------+
30
+-------------------------------------------------+


mysql> SELECT CURRENT_DATE();

+----------------+
CURRENT_DATE()
+----------------+
2009-03-31
+----------------+

mysql> SELECT RIGHT(CURRENT_DATE(),5);
+-------------------------+
RIGHT(CURRENT_DATE(),5)
+-------------------------+
03-31
+-------------------------+

mysql> select DATE_SUB(CURDATE(),INTERVAL 1 DAY) ;
+------------------------------------+
DATE_SUB(CURDATE(),INTERVAL 1 DAY)
+------------------------------------+
2009-03-30
+------------------------------------+

mysql> select count(*) as cnt from log where date >= DATE_SUB(CURDATE(),INTERVAL 1 DAY);


No comments:

Post a Comment