{"id":399,"date":"2015-10-07T12:52:14","date_gmt":"2015-10-07T17:52:14","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=399"},"modified":"2015-10-07T12:55:13","modified_gmt":"2015-10-07T17:55:13","slug":"date_trunc-for-mysql","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=399","title":{"rendered":"DATE_TRUNC for MySQL"},"content":{"rendered":"<p>Because somebody asked for it on Freenode:<\/p>\n<pre>\r\nCREATE FUNCTION DATE_TRUNC(field ENUM('microsecond', 'millisecond', 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year', 'decade', 'century', 'millennium'), source datetime(6))\r\nRETURNS datetime(6)\r\nDETERMINISTIC\r\nBEGIN\r\n  IF field IN ('millisecond') THEN SET source = source - INTERVAL MICROSECOND(source) % 1000 MICROSECOND; END IF;\r\n  IF field NOT IN ('microsecond', 'millisecond') THEN SET source = source - INTERVAL MICROSECOND(source) MICROSECOND; END IF;\r\n  IF field NOT IN ('microsecond', 'millisecond', 'second') THEN SET source = source - INTERVAL SECOND(source) SECOND; END IF;\r\n  IF field NOT IN ('microsecond', 'millisecond', 'second', 'minute') THEN SET source = source - INTERVAL MINUTE(source) MINUTE; END IF;\r\n  IF field NOT IN ('microsecond', 'millisecond', 'second', 'minute', 'hour') THEN SET source = source - INTERVAL HOUR(source) HOUR; END IF;\r\n  IF field NOT IN ('microsecond', 'millisecond', 'second', 'minute', 'hour', 'day') THEN SET source = source - INTERVAL DAYOFWEEK(source) - 1 DAY; END IF;\r\n  IF field NOT IN ('microsecond', 'millisecond', 'second', 'minute', 'hour', 'day', 'week') THEN SET source = source - INTERVAL DAY(source) - 1 DAY; END IF;\r\n  IF field IN ('quarter') THEN SET source = source - INTERVAL MONTH(source) % 3 - 1 MONTH; END IF;\r\n  IF field NOT IN ('microsecond', 'millisecond', 'second', 'minute', 'hour', 'week', 'day', 'month', 'quarter') THEN SET source = source - INTERVAL MONTH(source) - 1 MONTH; END IF;\r\n\r\n  -- Year ranges go from 1 - 10, e.g. 1961-1970, not 1960-1969. The third millenium started 2001, not 2000. If you want it the other way, remove the \"- 1\" from each of the following.\r\n  IF field IN ('decade') THEN SET source = source - INTERVAL YEAR(source) % 10 - 1 YEAR; END IF;\r\n  IF field IN ('century') THEN SET source = source - INTERVAL YEAR(source) % 100  - 1 YEAR; END IF;\r\n  IF field IN ('millennium') THEN SET source = source - INTERVAL YEAR(source) % 1000 - 1 YEAR; END IF;\r\n \r\n  RETURN source;\r\nEND\r\n<\/pre>\n<p>When called with the date &#8216;1996-02-29 12:28:53.123456&#8217;, returns the following:<\/p>\n<table>\n<tr>\n<th>FIELD<\/th>\n<th>Returned value<\/th>\n<\/tr>\n<tr>\n<td>MICROSECOND<\/td>\n<td>1996-02-29 12:28:53.123456<\/td>\n<\/tr>\n<tr>\n<td>MILLISECOND<\/td>\n<td>1996-02-29 12:28:53.123000<\/td>\n<\/tr>\n<tr>\n<td>SECOND<\/td>\n<td>1996-02-29 12:28:53.000000<\/td>\n<\/tr>\n<tr>\n<td>MINUTE<\/td>\n<td>1996-02-29 12:28:00.000000<\/td>\n<\/tr>\n<tr>\n<td>HOUR<\/td>\n<td>1996-02-29 12:00:00.000000<\/td>\n<\/tr>\n<tr>\n<td>DAY<\/td>\n<td>1996-02-29 00:00:00.000000<\/td>\n<\/tr>\n<tr>\n<td>WEEK<\/td>\n<td>1996-02-25 00:00:00.000000<\/td>\n<\/tr>\n<tr>\n<td>MONTH<\/td>\n<td>1996-02-01 00:00:00.000000<\/td>\n<\/tr>\n<tr>\n<td>QUARTER<\/td>\n<td>1996-01-01 00:00:00.000000<\/td>\n<\/tr>\n<tr>\n<td>YEAR<\/td>\n<td>1996-01-01 00:00:00.000000<\/td>\n<\/tr>\n<tr>\n<td>DECADE<\/td>\n<td>1991-01-01 00:00:00.000000<\/td>\n<\/tr>\n<tr>\n<td>CENTURY<\/td>\n<td>1901-01-01 00:00:00.000000<\/td>\n<\/tr>\n<tr>\n<td>MILLENNIUM<\/td>\n<td>1001-01-01 00:00:00.000000<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Because somebody asked for it on Freenode: CREATE FUNCTION DATE_TRUNC(field ENUM(&#8216;microsecond&#8217;, &#8216;millisecond&#8217;, &#8216;second&#8217;, &#8216;minute&#8217;, &#8216;hour&#8217;, &#8216;day&#8217;, &#8216;week&#8217;, &#8216;month&#8217;, &#8216;quarter&#8217;, &#8216;year&#8217;, &#8216;decade&#8217;, &#8216;century&#8217;, &#8216;millennium&#8217;), source datetime(6)) RETURNS datetime(6) DETERMINISTIC BEGIN IF field IN (&#8216;millisecond&#8217;) THEN SET source = source &#8211; INTERVAL MICROSECOND(source) % 1000 MICROSECOND; END IF; IF field NOT IN (&#8216;microsecond&#8217;, &#8216;millisecond&#8217;) THEN SET source [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[],"class_list":["post-399","post","type-post","status-publish","format-standard","hentry","category-mysql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-6r","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=399"}],"version-history":[{"count":4,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/399\/revisions"}],"predecessor-version":[{"id":403,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/399\/revisions\/403"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}