{"id":1081,"date":"2010-09-16T13:08:24","date_gmt":"2010-09-16T13:08:24","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=1081"},"modified":"2015-01-03T16:32:29","modified_gmt":"2015-01-03T16:32:29","slug":"mysql-function-to-convert-date-to-words","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/mysql-function-to-convert-date-to-words","title":{"rendered":"MySQL Function to Convert Date To Words"},"content":{"rendered":"<p>Recently I saw a MySQL Stored Function requirement on Experts-Exchange for converting date into some specific words format.<br \/>\nYou may find MySQL function for date to words conversion online; even udfs might be ready, but I decided to write my own.<br \/>\nI wrote this simple function mainly based on SELECT CASE to convert dates in to words as follows:<\/p>\n<p>mysql&gt;SELECT date_to_words(&#8216;2010-05-08&#8217;);<br \/>\nEighth Day of May Two Thousand Ten<\/p>\n<p>Download sql file below the code.<\/p>\n<blockquote><p>DELIMITER $$<\/p>\n<p>DROP FUNCTION IF EXISTS `date_to_words` $$<br \/>\nCREATE FUNCTION `date_to_words` (mydate DATE) RETURNS VARCHAR(100)<br \/>\nDETERMINISTIC<br \/>\nBEGIN<\/p>\n<p>\/* Converts date into words *\/<\/p>\n<p>DECLARE yr INT;<br \/>\nDECLARE dateval INT;<br \/>\nDECLARE thousand INT;<br \/>\nDECLARE hundred INT;<br \/>\nDECLARE tens INT;<\/p>\n<p>DECLARE tensword VARCHAR(10);<br \/>\nDECLARE onesword VARCHAR(10);<br \/>\nDECLARE thousandsword VARCHAR(20);<br \/>\nDECLARE hundredsword VARCHAR(20);<br \/>\nDECLARE datevalsword VARCHAR(20);<\/p>\n<p>SET yr=year(mydate);<br \/>\nSET dateval=day(mydate);<\/p>\n<p>\/* DAY TO WORDS *\/<\/p>\n<p>SELECT CASE dateval<br \/>\nWHEN 1 THEN &#8216;First&#8217;<br \/>\nWHEN 2 THEN &#8216;Second&#8217;<br \/>\nWHEN 3 THEN &#8216;Third&#8217;<br \/>\nWHEN 4 THEN &#8216;Fourth&#8217;<br \/>\nWHEN 5 THEN &#8216;Fifth&#8217;<br \/>\nWHEN 6 THEN &#8216;Sixth&#8217;<br \/>\nWHEN 7 THEN &#8216;Seventh&#8217;<br \/>\nWHEN 8 THEN &#8216;Eighth&#8217;<br \/>\nWHEN 9 THEN &#8216;Ninth&#8217;<br \/>\nWHEN 10 THEN &#8216;Tenth&#8217;<br \/>\nWHEN 11 THEN &#8216;Eleventh&#8217;<br \/>\nWHEN 12 THEN &#8216;Twelfth&#8217;<br \/>\nWHEN 13 THEN &#8216;Thirteenth&#8217;<br \/>\nWHEN 14 THEN &#8216;Fourteenth&#8217;<br \/>\nWHEN 15 THEN &#8216;Fifteenth&#8217;<br \/>\nWHEN 16 THEN &#8216;Sixteenth&#8217;<br \/>\nWHEN 17 THEN &#8216;Seventeenth&#8217;<br \/>\nWHEN 18 THEN &#8216;Eighteenth&#8217;<br \/>\nWHEN 19 THEN &#8216;Nineteenth&#8217;<br \/>\nWHEN 20 THEN &#8216;Twentieth&#8217;<br \/>\nWHEN 21 THEN &#8216;Twenty-first&#8217;<br \/>\nWHEN 22 THEN &#8216;Twenty-second&#8217;<br \/>\nWHEN 23 THEN &#8216;Twenty-third&#8217;<br \/>\nWHEN 24 THEN &#8216;Twenty-fourth&#8217;<br \/>\nWHEN 25 THEN &#8216;Twenty-fifth&#8217;<br \/>\nWHEN 26 THEN &#8216;Twenty-sixth&#8217;<br \/>\nWHEN 27 THEN &#8216;Twenty-seventh&#8217;<br \/>\nWHEN 28 THEN &#8216;Twenty-eighth&#8217;<br \/>\nWHEN 29 THEN &#8216;Twenty-ninth&#8217;<br \/>\nWHEN 30 THEN &#8216;Thirtieth&#8217;<br \/>\nWHEN 31 THEN &#8216;Thirty-first&#8217;<br \/>\nEND into datevalsword;<\/p>\n<p>\/* YEAR TO WORDS *\/<br \/>\nset thousand=floor(yr\/1000) ;<br \/>\nset yr = yr &#8211; thousand * 1000;<br \/>\nset hundred = floor(yr \/ 100);<br \/>\nset yr = yr &#8211; hundred * 100;<\/p>\n<p>IF (yr &gt; 19) THEN<br \/>\nset tens = floor(yr \/ 10);<br \/>\nset yr = yr mod 10;<br \/>\nELSE<br \/>\nset tens=0;<br \/>\nEND IF;<\/p>\n<p>SELECT CASE thousand<br \/>\nWHEN 1 THEN &#8216;One&#8217;<br \/>\nWHEN 2 THEN &#8216;Two&#8217;<br \/>\nWHEN 3 THEN &#8216;Three&#8217;<br \/>\nWHEN 4 THEN &#8216;Four&#8217;<br \/>\nWHEN 5 THEN &#8216;Five&#8217;<br \/>\nWHEN 6 THEN &#8216;Six&#8217;<br \/>\nWHEN 7 THEN &#8216;Seven&#8217;<br \/>\nWHEN 8 THEN &#8216;Eight&#8217;<br \/>\nWHEN 9 THEN &#8216;Nine&#8217;<br \/>\nEND INTO thousandsword;<br \/>\nSET thousandsword=concat(thousandsword,&#8217; Thousand &#8216;);<\/p>\n<p>SELECT CASE hundred<br \/>\nWHEN 0 then &#8221;<br \/>\nWHEN 1 THEN &#8216;One&#8217;<br \/>\nWHEN 2 THEN &#8216;Two&#8217;<br \/>\nWHEN 3 THEN &#8216;Three&#8217;<br \/>\nWHEN 4 THEN &#8216;Four&#8217;<br \/>\nWHEN 5 THEN &#8216;Five&#8217;<br \/>\nWHEN 6 THEN &#8216;Six&#8217;<br \/>\nWHEN 7 THEN &#8216;Seven&#8217;<br \/>\nWHEN 8 THEN &#8216;Eight&#8217;<br \/>\nWHEN 9 THEN &#8216;Nine&#8217;<br \/>\nEND INTO hundredsword;<br \/>\nif (hundredsword&lt;&gt;&#8221;) then<br \/>\nSET hundredsword=concat(hundredsword,&#8217; Hundred &#8216;) ;<br \/>\nelse<br \/>\nset hundredsword=&#8221;;<br \/>\nend if;<\/p>\n<p>\/*TENS To WORDS*\/<br \/>\nSELECT CASE tens<br \/>\nWHEN 2 THEN &#8216;Twenty&#8217;<br \/>\nWHEN 3 THEN &#8216;Thirty&#8217;<br \/>\nWHEN 4 THEN &#8216;Fourty&#8217;<br \/>\nWHEN 5 THEN &#8216;Fifty&#8217;<br \/>\nWHEN 6 THEN &#8216;Sixty&#8217;<br \/>\nWHEN 7 THEN &#8216;Seventy&#8217;<br \/>\nWHEN 8 THEN &#8216;Eigthy&#8217;<br \/>\nWHEN 9 THEN &#8216;Ninety&#8217;<br \/>\nELSE &#8221;<br \/>\nEND INTO tensword;<\/p>\n<p>\/*ONES To WORDS*\/<br \/>\nSELECT CASE yr<br \/>\nWHEN 0 THEN &#8221;<br \/>\nWHEN 1 THEN &#8216;One&#8217;<br \/>\nWHEN 2 THEN &#8216;Two&#8217;<br \/>\nWHEN 3 THEN &#8216;Three&#8217;<br \/>\nWHEN 4 THEN &#8216;Four&#8217;<br \/>\nWHEN 5 THEN &#8216;Five&#8217;<br \/>\nWHEN 6 THEN &#8216;Six&#8217;<br \/>\nWHEN 7 THEN &#8216;Seven&#8217;<br \/>\nWHEN 8 THEN &#8216;Eight&#8217;<br \/>\nWHEN 9 THEN &#8216;Nine&#8217;<br \/>\nWHEN 10 THEN &#8216;Ten&#8217;<br \/>\nWHEN 11 THEN &#8216;Eleven&#8217;<br \/>\nWHEN 12 THEN &#8216;Twelve&#8217;<br \/>\nWHEN 13 THEN &#8216;Thirteen&#8217;<br \/>\nWHEN 14 THEN &#8216;Fourteen&#8217;<br \/>\nWHEN 15 THEN &#8216;Fifteen&#8217;<br \/>\nWHEN 16 THEN &#8216;Sixteen&#8217;<br \/>\nWHEN 17 THEN &#8216;Seventeen&#8217;<br \/>\nWHEN 18 THEN &#8216;Eighteen&#8217;<br \/>\nWHEN 19 THEN &#8216;Nineteen&#8217;<br \/>\nEND into onesword;<\/p>\n<p>return concat(datevalsword, &#8216; Day of &#8216;, date_format(mydate,&#8217;%M&#8217;),&#8217; &#8216;,thousandsword,hundredsword, tensword,&#8217; &#8216;,onesword);<br \/>\nEND $$<\/p>\n<p>DELIMITER ;<\/p><\/blockquote>\n<p><strong>Download SQL Code<\/strong> for converting date to words.<a href=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2010\/09\/date_to_words.sql_.txt\" target=\"_blank\">date_to_words.sql<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Recently I saw a MySQL Stored Function requirement on Experts-Exchange for converting date into some specific words format. You may find MySQL function for date to words conversion online; even&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,378],"tags":[24,222,223,44,427],"class_list":{"0":"post-1081","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-scripts-mysql","8":"tag-code","9":"tag-convert-date-to-words","10":"tag-date-to-words","11":"tag-function","12":"tag-mysql"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=1081"}],"version-history":[{"count":2,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1081\/revisions"}],"predecessor-version":[{"id":1992,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1081\/revisions\/1992"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=1081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=1081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=1081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}