{"id":169,"date":"2010-07-06T20:37:37","date_gmt":"2010-07-07T01:37:37","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=169"},"modified":"2021-03-08T20:06:41","modified_gmt":"2021-03-09T01:06:41","slug":"powerbuilder-ms-sqlserver-datetime-methods","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/powerbuilder-ms-sqlserver-datetime-methods\/","title":{"rendered":"PowerBuilder &#038; MS SQLServer datetime methods"},"content":{"rendered":"<p>If you have scheduling functionality in your application sooner or later you are faced with spanning dates.  By this I mean starting something on one day and ending it on another.  Powerbuilder is not very good at this, especially if you have something which starts before midnight and ends after midnight.  SQL Server provides good functionality for date manipulation.  Here are a couple of functions you may find useful.<\/p>\n<pre name=\"code\" class=\"VB\">f_datetime_after (datetime adt, long, al_amt, string as_datepart)\n\n\/\/returns the datetime after a set interval from a starting datetime\n\/\/cannot use powerbuilder since it cannot span days\n\/\/ valid dateparts are:\n\/*Year\nquarter\nmonth\ndayofyear\nday\nweek\nweekday\nhour\nminute\nsecond\nmillisecond\nmicrosecond\nnanosecond*\/\nstring ls_sql\ndatetime ldt_end\nDECLARE get_diff DYNAMIC CURSOR FOR SQLSA ;\n\/\/ Calculate the end date\nls_sql = &#039;SELECT DATEADD(&#039; + as_datepart + &#039;, &#039; + string(al_amt) +  &quot;, &#039;&quot; + string(adt) + &quot;&#039;)&quot;\nPREPARE SQLSA FROM :ls_sql ;\nOPEN DYNAMIC get_diff ;\nFETCH get_diff INTO :ldt_end ;\nCLOSE get_diff ;\nRETURN ldt_end<\/pre>\n<pre name=\"code\" class=\"VB\">f_difference_in_minutes (datetime adt_start, datetime adt_end)\n\/\/returns the difference between the two times in minutes.\nlong ll_dur_qty\nstring ls_sql\nDECLARE get_seconds_diff DYNAMIC CURSOR FOR SQLSA ;\n\n\/\/ Calculate the difference\nls_sql= &#039;select DateDiff(ss, &quot;&#039; + string(adt_start) + &#039;&quot;, &quot;&#039; + string(adt_stop) + &#039;&quot;)&#039;\nPREPARE SQLSA FROM :ls_sql ;\nOPEN DYNAMIC get_seconds_diff ;\nFETCH get_seconds_diff INTO :ll_dur_qy ;\nCLOSE get_seconds_diff ;\n\nll_dur_qy = ll_dur_qy \/ 60 \/\/ convert to minutes\nRETURN ll_dur_qty<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you have scheduling functionality in your application sooner or later you are faced with spanning dates. By this I mean starting something on one day and ending it on another. Powerbuilder is not very good at this, especially if you have something which starts before midnight and ends after midnight. SQL Server provides good&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,3,10],"tags":[12,16,15],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/169"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":17,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":1753,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/169\/revisions\/1753"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}