{"id":1731,"date":"2007-09-07T17:20:00","date_gmt":"2007-09-07T12:20:00","guid":{"rendered":"http:\/\/chris.dwan.org\/?p=1731"},"modified":"2007-09-07T17:20:00","modified_gmt":"2007-09-07T12:20:00","slug":"nerd-times","status":"publish","type":"post","link":"https:\/\/chris.dwan.org\/?p=1731","title":{"rendered":"Nerd times"},"content":{"rendered":"<p>Gotta brag.<\/p>\n<p><!--more nerdy bragging-->We&#8217;ve got an application that uses MySQL to store lists of things.  There was a case today where we suddenly wanted to sort by a particular date that was in a field in the table.  <\/p>\n<p>Unfortunately, we don&#8217;t use a &#8220;date&#8221; field in mysql for this information.  We use a string of 30 characters.  Yes, yes.  Kill me.  Trust me, if I got to fix everything in our code that sucks, that would be one of the very last changes.  We then happen to stick characters into that string that a human might read as a date, like this: <code>Mon Aug 27 00:43:21 2007 <\/code>.  If you simply sort on it, you end up sorting by day of week.  &#8220;Fri&#8221; comes before &#8220;Thu&#8221;.  Not very useful.<\/p>\n<p>Fortunately, mysql has a function for exactly this situation:  STR_TO_DATE.  Here&#8217;s the new code:<\/p>\n<pre>my $sth = $dbh->prepare( qq{ SELECT STR_TO_DATE(stime, '%a %b %e %H:%i:%s %Y') as the_date, jobnumber,\r\n                                        exit_status, wallclk, cpu, jobs, jobname \r\n                                 FROM dms_jobs\r\n                                 WHERE\r\n                                 username = ? order by the_date desc\r\n                               } ) ;<\/pre>\n<p>I&#8217;m really glad that I looked in the mysql docs to see if they had a solution before embarking on some sort of regular expression odyssey in perl.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gotta brag.<\/p>\n<p> <span style=\"color:#777\"> . . . &rarr; Read More: <a href=\"https:\/\/chris.dwan.org\/?p=1731\">Nerd times<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/chris.dwan.org\/index.php?rest_route=\/wp\/v2\/posts\/1731"}],"collection":[{"href":"https:\/\/chris.dwan.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chris.dwan.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chris.dwan.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chris.dwan.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1731"}],"version-history":[{"count":0,"href":"https:\/\/chris.dwan.org\/index.php?rest_route=\/wp\/v2\/posts\/1731\/revisions"}],"wp:attachment":[{"href":"https:\/\/chris.dwan.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chris.dwan.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chris.dwan.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}