{"id":13,"date":"2007-07-12T11:03:35","date_gmt":"2007-07-12T09:03:35","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=13"},"modified":"2007-07-12T11:03:35","modified_gmt":"2007-07-12T09:03:35","slug":"data-conversion-woes-pt-2","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/07\/12\/data-conversion-woes-pt-2\/","title":{"rendered":"Data Conversion Woes, pt 2"},"content":{"rendered":"<p>In <a href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/07\/10\/data-conversion-woes\/\" title=\"Part 1\">Part 1<\/a> I discussed why some queries that look like they should work fail with a conversion error. In this part, I&#8217;m going to go over some solutions to the problem.<\/p>\n<ol>\n<li> Probably best solution, at least in the long term is, as I mentioned previously, to correct the data type of the column and to remove the data that doesn&#8217;t fit. This is probably, however, the hardest of the solutions to implement, due to the potential impact.<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<ol>\n<li>Don&#8217;t do the conversion. If you can get away without converting the data types, then do so. In my example, because the date was in the format yyyy\/mm\/dd, a string comparison produces the expected results.\n<pre>SELECT * FROM tbl_TestingConversion\n WHERE ISDate(TheDate)=1\n AND TheDate &gt; '2007\/07\/01'<\/pre>\n<\/li>\n<li>Use a case statement in the WHERE clause. Turn the non-date values to NULL  and then compare. The downside is that SQL will be completely unable to use an index if one exists on the compared column.\n<pre>SELECT *\n  from tbl_TestingConversion\n  where CAST(CASE ISDate(TheDate) WHEN 1 THEN TheDate ELSE Null END AS DATETIME) &gt; '2007\/07\/01'<\/pre>\n<\/li>\n<\/ol>\n<p>That&#8217;s a few ways to work around conversion errors. Anyone else have a preferred method that they&#8217;d like to share?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Part 1 I discussed why some queries that look like they should work fail with a conversion error. In this part, I&#8217;m going to go over some solutions to the problem. Probably best solution, at least in the long&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2007\/07\/12\/data-conversion-woes-pt-2\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,17],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-t-sql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-d","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}