{"id":142,"date":"2013-06-19T12:21:00","date_gmt":"2013-06-19T04:21:00","guid":{"rendered":"http:\/\/matnet.my\/blog\/?p=142"},"modified":"2013-06-19T12:25:13","modified_gmt":"2013-06-19T04:25:13","slug":"mysql-order-by-case","status":"publish","type":"post","link":"https:\/\/matnet.my\/blog\/2013\/06\/mysql-order-by-case\/","title":{"rendered":"MySQL order by case"},"content":{"rendered":"<p>This post is referring from my previous post regarding Sort MySQL Varchar Column<\/p>\n<p>The issue is the IC prefix is not only G, there are new prefix with RF.<\/p>\n<p>So when I&#8217;m sorting with <strong>ORDER BY SUBSTR(IC,2)+0 ASC<\/strong><\/p>\n<p>It will read from second letter of G12345 that is no 1<\/p>\n<p>But if the prefix is RF12345 so it will read at F so the sort will not work.<\/p>\n<p>So now i need to order by case,\u00a0 If IC with prefix G the substr should use <strong>ORDER BY SUBSTR(IC,2)+0 ASC<\/strong><\/p>\n<p>And IC prefix with RF the substr should use <strong>ORDER BY SUBSTR(IC,3)+0 ASC<\/strong><\/p>\n<p>I use this statement to solve the problem:<\/p>\n<p><strong> ORDER by<\/strong><br \/>\n<strong>\u00a0\u00a0\u00a0 CASE<\/strong><br \/>\n<strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 when ic like &#8216;RF%&#8217; THEN SUBSTR(nobadan,3)+0<\/strong><br \/>\n<strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 when ic like &#8216;G%&#8217; THEN SUBSTR(nobadan,2)+0<\/strong><br \/>\n<strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 END ASC<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is referring from my previous post regarding Sort MySQL Varchar Column The issue is the IC prefix is &hellip; <a href=\"https:\/\/matnet.my\/blog\/2013\/06\/mysql-order-by-case\/\" class=\"more-link\">More <span class=\"screen-reader-text\">MySQL order by case<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,3],"tags":[],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-bsd-nix","category-www","standard"],"_links":{"self":[{"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":6,"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/matnet.my\/blog\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}