{"id":592,"date":"2013-05-24T15:52:07","date_gmt":"2013-05-24T20:52:07","guid":{"rendered":"https:\/\/danvanfleet.com\/?p=592"},"modified":"2013-06-03T08:10:26","modified_gmt":"2013-06-03T13:10:26","slug":"sql-convert-yyyymmdd-integer-or-string-to-a-date-that-works-in-softpro","status":"publish","type":"post","link":"https:\/\/danvanfleet.com\/index.php\/sql-convert-yyyymmdd-integer-or-string-to-a-date-that-works-in-softpro\/","title":{"rendered":"SQL convert &quot;yyyyMMdd&quot; integer or string to a Date that works in SoftPro"},"content":{"rendered":"<p>The last few weeks I&#8217;ve been working with Web based Reporting for <a href=\"http:\/\/SoftProCorp.com\" target=\"_blank\">SoftPro<\/a> ProForm.&nbsp; It&#8217;s been fun creating management review pages with dials and lights.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2013\/05\/image9.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; margin: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/i0.wp.com\/danvanfleet.com\/wp-content\/uploads\/2013\/05\/image_thumb9.png?resize=530%2C152\" width=\"530\" height=\"152\"><\/a> <\/p>\n<p>I&#8217;ve enjoyed combining multiple report outputs on one concise easy to ready web page.&nbsp; Along the way, it&#8217;s been necessary to display <a href=\"http:\/\/SoftProCorp.com\" target=\"_blank\">SoftPro<\/a> Database formatted date fields in a grid.&nbsp; They come through in 20130524 or &#8220;yyyyMMdd&#8221; format.&nbsp; It was easy enough to format that in a grid as &#8220;yyyy-MM-dd&#8221; format which worked until I started exporting the grids to Excel, when the formatting failed.&nbsp; So I began a search to fix it, at the SQL level.<\/p>\n<p><!--more--><\/p>\n<p>There are a ton of examples of how to do this, they don&#8217;t work for <a href=\"http:\/\/SoftProCorp.com\" target=\"_blank\">SoftPro<\/a> Standard or Enterprise Databases, as when nothing is in the field, a &#8220;0&#8221; zero is stored, that caused a basic conversion to fail. <\/p>\n<p>Out came the Case statement with a solution!<\/p>\n<div id=\"codeSnippetWrapper\">\n<div style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\" id=\"codeSnippet\">\n<pre style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">SELECT<\/span> <span style=\"color: #0000ff\">CASE<\/span> <span style=\"color: #0000ff\">WHEN<\/span> <span style=\"color: #0000ff\">Search<\/span>.OrdDate = 0 <span style=\"color: #0000ff\">THEN<\/span> <span style=\"color: #0000ff\">NULL<\/span> <\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">ELSE<\/span> <span style=\"color: #0000ff\">CAST<\/span>(<span style=\"color: #0000ff\">CAST<\/span>(<span style=\"color: #0000ff\">Search<\/span>.OrdDate <span style=\"color: #0000ff\">AS<\/span> <span style=\"color: #0000ff\">CHAR<\/span>(8)) <span style=\"color: #0000ff\">AS<\/span> <span style=\"color: #0000ff\">Date<\/span>) <span style=\"color: #0000ff\">END<\/span> <span style=\"color: #0000ff\">as<\/span> OrderDate <span style=\"color: #0000ff\">From<\/span> Search<\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<p>So we check the field to see if it&#8217;s a zero, if so, send back a null to a column called OrderDate, if it&#8217;s not a zero, cast or change the field from being an integer to a Date.&nbsp; This routine does a similar thing to the Crystal Reports Formula<\/p>\n<div id=\"codeSnippetWrapper\">\n<div style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\" id=\"codeSnippet\">\n<pre style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\">Numbervar Year1:=<span style=\"color: #0000ff\">Truncate<\/span>( {DatesTimes.OrdDate} \/ 10000 );<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\">Numbervar Month1:=<span style=\"color: #0000ff\">Truncate<\/span>(({DatesTimes.OrdDate}-(Year1*10000))\/100);<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\">Numbervar Day1:=<span style=\"color: #0000ff\">Truncate<\/span>({DatesTimes.OrdDate}-<span style=\"color: #0000ff\">Truncate<\/span>({DatesTimes.OrdDate}\/100)*100);<\/pre>\n<p><!--CRLF--><\/p>\n<pre style=\"border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px\"><span style=\"color: #0000ff\">Date<\/span>(Year1,Month1,Day1);<\/pre>\n<p><!--CRLF--><\/div>\n<\/div>\n<\/p>\n<p>I wonder if the same methodology would work in a Crystal Selection Formula, but since I started doing these Web based reports, I don&#8217;t expect to live in Crystal for most reporting needs.&nbsp; This Live Report concept for <a href=\"http:\/\/SoftProCorp.com\" target=\"_blank\">SoftPro<\/a> products is just awesome, we can export the grid to Excel with perfect formatting, add headers and footers if necessary, and it&#8217;s all much faster than generating a Crystal Report.&nbsp; Most reports have been running in a few seconds, so in under a minute you can look at 8 or 10 different reports.&nbsp; Or just load up the Right Now page to see the aggregate numbers of selected reports in a dashboard style.<\/p>\n<p>If you need to calculate the difference between dates, pull out the SQL command DATEDIFF(day, Date1CaseStatement, Date2CaseStatement) as DateDifferenceColumn.&nbsp; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The last few weeks I&#8217;ve been working with Web based Reporting for SoftPro ProForm.&nbsp; It&#8217;s been fun creating management review pages with dials and lights. I&#8217;ve enjoyed combining multiple report outputs on one concise easy to ready web page.&nbsp; Along the way, it&#8217;s been necessary to display SoftPro Database formatted date fields in a grid.&nbsp; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_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},"jetpack_post_was_ever_published":false},"categories":[23],"tags":[27,13,17],"class_list":["post-592","post","type-post","status-publish","format-standard","hentry","category-softpro-2","tag-reports","tag-sql","tag-tips","entry"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4wyVb-9y","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/posts\/592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/comments?post=592"}],"version-history":[{"count":1,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/posts\/592\/revisions\/607"}],"wp:attachment":[{"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/media?parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/categories?post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danvanfleet.com\/index.php\/wp-json\/wp\/v2\/tags?post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}