{"id":1094,"date":"2011-07-05T04:08:47","date_gmt":"2011-07-04T22:08:47","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1094"},"modified":"2016-01-07T11:31:06","modified_gmt":"2016-01-07T05:31:06","slug":"fun-with-dates","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1094","title":{"rendered":"Fun with Dates"},"content":{"rendered":"<p>Don&#8217;t get your hopes up, this has nothing to with date nights !! \u00c2\u00a0 \ud83d\ude09 \u00c2\u00a0Now we know how to find number of days between two dates, add days and so on. \u00c2\u00a0Few days ago I needed to list out all the valid dates between two given dates. \u00c2\u00a0Here is a quick way to do that :<\/p>\n<p><span style=\"color: #ff9900;\">with temp1(mydate) as<\/span><br \/>\n<span style=\"color: #ff9900;\">(<\/span><br \/>\n<span style=\"color: #ff9900;\">values date(&#8216;2011-01-01&#8217;)<\/span><br \/>\n<span style=\"color: #ff9900;\">union all<\/span><br \/>\n<span style=\"color: #ff9900;\">select date(mydate) + 1 days<\/span><br \/>\n<span style=\"color: #ff9900;\">from temp1<\/span><br \/>\n<span style=\"color: #ff9900;\">where date (mydate) &lt; date (&#8216;2011-12-31&#8217;)<\/span><br \/>\n<span style=\"color: #ff9900;\">)<\/span><br \/>\n<span style=\"color: #ff9900;\">select date(mydate)<\/span><br \/>\n<span style=\"color: #ff9900;\">from temp1<\/span><br \/>\n<span style=\"color: #ff9900;\">; <\/span><br \/>\nThis will list all dates between 2011-01-01 and 2011-12-31.<\/p>\n<p>Just to add little more fun to this, what if you need to list out all the week number, first day of the week and last day of the week for the same date range :<\/p>\n<p><span style=\"color: #ff9900;\">with temp1(mydate) as<\/span><br \/>\n<span style=\"color: #ff9900;\">(<\/span><br \/>\n<span style=\"color: #ff9900;\">values date(&#8216;2011-01-01&#8217;)<\/span><br \/>\n<span style=\"color: #ff9900;\">union all<\/span><br \/>\n<span style=\"color: #ff9900;\">select date(mydate) + 1 days<\/span><br \/>\n<span style=\"color: #ff9900;\">from temp1<\/span><br \/>\n<span style=\"color: #ff9900;\">where date (mydate) &lt; date (&#8216;2011-12-31&#8217;)<\/span><br \/>\n<span style=\"color: #ff9900;\">)<\/span><\/p>\n<p><span style=\"color: #ff9900;\">select WEEK(date(mydate))as week_no,<\/span><br \/>\n<span style=\"color: #ff9900;\">date(mydate) &#8211; (dayofweek(date(mydate)) &#8211; 1) days as first_day_of_week,<\/span><br \/>\n<span style=\"color: #ff9900;\">date(mydate) + (7 &#8211; dayofweek(date(mydate))) days as last_day_of_week<\/span><br \/>\n<span style=\"color: #ff9900;\">from temp1<\/span><br \/>\n<span style=\"color: #ff9900;\">group by WEEK(date(mydate))<\/span><br \/>\n<span style=\"color: #ff9900;\">,date(mydate) &#8211; (dayofweek(date(mydate)) &#8211; 1) days<\/span><br \/>\n<span style=\"color: #ff9900;\">,date(mydate) + (7 &#8211; dayofweek(date(mydate))) days<\/span><br \/>\n<span style=\"color: #ff9900;\">;<\/span><\/p>\n<p>You can build numerous iterations from this. Happy SQL&#8217;ng ! \u00c2\u00a0 Comment better ways to do the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Don&#8217;t get your hopes up, this has nothing to with date nights !! \u00c2\u00a0 \ud83d\ude09 \u00c2\u00a0Now we know how to find number of days between two dates, add days and so on. \u00c2\u00a0Few days ago I needed to list out all the valid dates between two given dates. \u00c2\u00a0Here is a quick way to do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[3,358],"tags":[943,629,636,632,637,635,631,633,634,630],"class_list":["post-1094","post","type-post","status-publish","format-standard","hentry","category-db2","category-how-to","tag-db2","tag-db2-date-functions","tag-db2-fun-with-dates","tag-first-day-of-the-week","tag-fun-with-date","tag-get-all-valid-date-between-two-dates","tag-group-by-weeks","tag-last-day-of-the-week","tag-list-all-dates","tag-list-all-days"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1094"}],"version-history":[{"count":5,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1094\/revisions"}],"predecessor-version":[{"id":1502,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1094\/revisions\/1502"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}