* MySQL SQL 日付関連 [#c3d6086c]
** 月末 [#p6cca634]
SELECT LAST_DAY(NOW());
** 月初 [#ca6972b7]
SELECT DATE_FORMAT(NOW(), '%Y-%m-01');
** 2週間後 (INTERVAL) [#e2f759bb]
SELECT NOW() + INTERVAL 2 WEEK;
** 日数の差 [#m27fd466]
SELECT DATEDIFF(CAST('2014-12-31' AS date), CAST('2014-11-01' AS date));
=> 60
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_datediff
** タイムスタンプを日付にキャスト [#z19f3b30]
SELECT DATE(NOW());
** 部分フィールドの取得 [#x1bd1911]
SELECT EXTRACT(YEAR FROM '1999-07-02');
*** INT(またはString)型の年月日からDate型の日付を作る [#hd0a8dc5]
SELECT STR_TO_DATE(CONCAT(year,'-',month,'-',day), '%Y-%c-%e') AS date FROM t1;
- http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_str-to-date
- http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
** 書式指定 DATE_FOMRMAT() [#y1198289]
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:00');
+-----------------------------------------+
| DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:00') |
+-----------------------------------------+
| 2015-06-02 03:41:00 |
+-----------------------------------------+
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
** 参考 [#vf47705a]
- http://dev.mysql.com/doc/refman/5.1-olh/ja/date-and-time-functions.html
- http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html