{"id":4788,"date":"2021-02-19T11:48:01","date_gmt":"2021-02-19T03:48:01","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=4788"},"modified":"2021-02-20T08:05:21","modified_gmt":"2021-02-20T00:05:21","slug":"gbase-8a-%e5%ae%9e%e7%8e%b0oracle%e9%87%8cmonths_between%e5%8a%9f%e8%83%bd%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.gbase8.cn\/en\/4788","title":{"rendered":"\u5357\u5927\u901a\u7528GBase 8a \u5b9e\u73b0oracle\u91ccmonths_between\u529f\u80fd\u51fd\u6570"},"content":{"rendered":"<p>\u6839\u636eOracle\u7684\u89c4\u5b9a\uff0cmonths_bwtween\u8fd4\u56de2\u4e2a\u65e5\u671f\u76f8\u5dee\u7684\u6708\u6570\uff0c\u672c\u6587\u4ecb\u7ecd\u5728GBase 8a\u6570\u636e\u5e93\u96c6\u7fa4\u91cc\u5982\u4f55\u5b9e\u73b0\u8be5\u529f\u80fd\u51fd\u6570\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Directory Navigation<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2ad7b27316e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2ad7b27316e\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.gbase8.cn\/en\/4788\/#%E5%88%86%E6%9E%90\" >Analyse<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.gbase8.cn\/en\/4788\/#%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%B3%95\" >\u5b9e\u73b0\u65b9\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.gbase8.cn\/en\/4788\/#SQL%E5%BD%A2%E5%BC%8F\" >SQL\u5f62\u5f0f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.gbase8.cn\/en\/4788\/#%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0UDF%E6%A0%BC%E5%BC%8F\" >\u81ea\u5b9a\u4e49\u51fd\u6570UDF\u683c\u5f0f<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.gbase8.cn\/en\/4788\/#%E6%89%A7%E8%A1%8C%E6%95%88%E6%9E%9C\" >\u6267\u884c\u6548\u679c<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.gbase8.cn\/en\/4788\/#C%E7%BC%96%E5%86%99%E7%9A%84UDF%E6%96%B9%E6%A1%88\" >C\u7f16\u5199\u7684UDF\u65b9\u6848<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%88%86%E6%9E%90\"><\/span>Analyse<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6839\u636eOracle\u89c4\u5b9a\uff0c\u5982\u679c\u65e5\u671f1\u665a\u4e8e\u65e5\u671f2\uff0c\u5219\u8fd4\u56de\u6b63\u6570\uff0c\u5426\u5219\u8fd4\u56de\u8d1f\u6570\u3002<br>\u5982\u679c2\u4e2a\u65e5\u671f\u7684\u5929\u90e8\u5206\u76f8\u540c\uff0c\u6216\u8005\u90fd\u662f\u6240\u5728\u6708\u7684\u6700\u540e\u4e00\u5929\uff0c\u90a3\u4e48\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6574\u6570(\u5c0f\u6570\u90e8\u5206\u4e3a0\uff09\u3002<br>\u5426\u5219\u4ee531\u5929\u4e3a\u57fa\u6570\uff0c\u8ba1\u7b97\u5c0f\u6570\u90e8\u5206\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E7%8E%B0%E6%96%B9%E6%B3%95\"><\/span>\u5b9e\u73b0\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u901a\u8fc7year\u548cmonth\u51fd\u6570\u53ef\u4ee5\u5206\u522b\u62ff\u5230\u65e5\u671f\u7684\u5e74\u548c\u6708\uff0c\u5176\u4e2d\u6bcf\u5e74\u662f21\u4e2a\u6708\uff0c\u6240\u4ee5\u6574\u6570\u90e8\u5206\u8ba1\u7b97\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(year(date1)-year(date2))*12\n+\n(month(date1)-month(date2))<\/code><\/pre>\n\n\n\n<p>\u5c0f\u6570\u90e8\u5206\uff0c\u9700\u8981\u5148\u5224\u65ad\u662f\u5426\u4e3a\u6240\u5728\u6708\u7684\u6700\u540e\u4e00\u5929\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>last_day(date)=date<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"SQL%E5%BD%A2%E5%BC%8F\"><\/span>SQL\u5f62\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u63a8\u8350\u65b9\u6848\uff0c\u867d\u7136\u770b\u4e0a\u53bb\u6709\u70b9\u591a\uff0c\u4f46\u65e0\u6027\u80fd\u635f\u5931\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select   \r\n  (year('2020-01-30')-year('2020-02-29'))*12 \/* \u5e74\u5dee*12 *\/\r\n  +\r\n  (month('2020-01-30')-month('2020-02-29')) \/* \u6708\u5dee *\/\r\n  +\r\n  if(last_day('2020-01-30')='2020-01-30' and last_day('2020-02-29')='2020-02-29'  \/* \u65e5\u5dee\uff0c\u5982\u679c\u90fd\u662f\u6240\u5728\u6708\u6700\u540e\u4e00\u5929\uff0c\u5219\u8fd4\u56de0 *\/\r\n    ,0\r\n    ,(day('2020-01-30')-day('2020-02-29'))\/31\r\n  )\r mm\n  from dual;  <\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase> select      (year('2020-01-30')-year('2020-02-29'))*12    +   (month('2020-01-30')-month('2020-02-29'))    +   if(last_day('2020-01-30')='2020-01-30' and last_day('2020-02-29')='2020-02-29'       ,0     ,(day('2020-01-30')-day('2020-02-gbase> select\r\n    ->   (year('2020-01-30')-year('2020-02-29'))*12 \/* \u5e74\u5dee*12 *\/\r\n    ->   +\r\n    ->   (month('2020-01-30')-month('2020-02-29')) \/* \u6708\u5dee *\/\r\n    ->   +\r\n    ->   if(last_day('2020-01-30')='2020-01-30' and last_day('2020-02-29')='2020-02-29'  \/* \u65e5\u5dee\uff0c\u5982\u679c\u90fd\u662f\u6240\u5728\u6708\u6700\u540e\u4e00\u5929\uff0c\u5219\u8fd4\u56de0 *\/\r\n    ->     ,0\r\n    ->     ,(day('2020-01-30')-day('2020-02-29'))\/31\r\n    ->   )\r\n    ->  mm\r\n    ->   from dual;\r\n+---------+\r\n| mm      |\r\n+---------+\r\n| -0.9677 |\r\n+---------+\r\n1 row in set (Elapsed: 00:00:00.01)\r\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0UDF%E6%A0%BC%E5%BC%8F\"><\/span>\u81ea\u5b9a\u4e49\u51fd\u6570UDF\u683c\u5f0f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5982\u679c\u5c11\u91cf\u4f7f\u7528\uff0c\u4e14\u4e0d\u662f\u6027\u80fd\u5173\u952e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>drop function if exists months_between;\r\ndelimiter \/\/\r\ncreate function months_between(date1 date,date2 date) returns decimal(64,9)\r\nbegin\r\n  declare dd decimal(64,9);\r\nselect   \r\n  (year(date1)-year(date2))*12 \/* \u5e74\u5dee*12 *\/\r\n  +\r\n  (month(date1)-month(date2)) \/* \u6708\u5dee *\/\r\n  +\r\n  if(last_day(date1)=date1 and last_day(date2)=date2  \/* \u65e5\u5dee\uff0c\u5982\u679c\u90fd\u662f\u6240\u5728\u6708\u6700\u540e\u4e00\u5929\uff0c\u5219\u8fd4\u56de0 *\/\r\n    ,0\r\n    ,(day(date1)-day(date2))\/31\r\n  )\r\n  \r\n  into dd;\r\n  return dd;\r\nend \/\/\r\ndelimiter ;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%89%A7%E8%A1%8C%E6%95%88%E6%9E%9C\"><\/span>\u6267\u884c\u6548\u679c<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u91c7\u7528\u4e86\u548cOracle\u76f8\u540c\u7684\u5199\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase> select months_between(\r\n    ->   to_date('2020-01-28','yyyy-mm-dd'),\r\n    ->   to_date('2020-02-29','yyyy-mm-dd')\r\n    -> ) a\r\n    -> from dual;\r\n+--------------+\r\n| a            |\r\n+--------------+\r\n| -1.032258064 |\r\n+--------------+\r\n1 row in set (Elapsed: 00:00:00.01)\r\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"C%E7%BC%96%E5%86%99%E7%9A%84UDF%E6%96%B9%E6%A1%88\"><\/span>C\u7f16\u5199\u7684UDF\u65b9\u6848<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u8be5\u65b9\u6848\u91c7\u7528C\u6765\u5b9e\u73b0\uff0c\u6027\u80fd\u6709\u4fdd\u969c\uff0c\u4f46\u9700\u8981\u989d\u5916\u7684\u7f16\u8bd1\u548c\u90e8\u7f72\uff0c\u5efa\u8bae\u5df2\u7ecf\u4f7f\u7528\u4e86CUDF\u6216\u8005\u5927\u91cf\u4f7f\u7528\u4e14\u6027\u80fd\u8981\u6c42\u9ad8\u7684\u60c5\u51b5\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>C\u7684UDF\u7f16\u8bd1\u548c\u90e8\u7f72\u65b9\u6cd5\uff0c\u8bf7\u53c2\u8003GBase\u7684\u4ea7\u54c1\u624b\u518c\u3002\u518d\u6b21\u63d0\u9192\uff0c\u4e0d\u5b8c\u5584\u7684C\u4ee3\u7801\uff0c\u6709\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u5b95\u673a\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u53ea\u662f\u6837\u4f8b\uff0c\u5982\u679c\u5728\u6b63\u5f0f\u9879\u76ee\u4f7f\u7528\uff0c\u8bf7\u505a\u5927\u6570\u636e\u91cf\u7684\u957f\u65f6\u95f4\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;gbase_global.h>\n#include &lt;gbase_sys.h>\n#include &lt;m_string.h>\n#include &lt;gbase.h>\n#include &lt;string.h>\n#include &lt;time.h>\n#include &lt;gbase.h>\n\n\nint is_end(struct tm timestru)\n{\n\tunsigned char x&#91;13]={0,31,28,31,30,31,30,31,31,30,31,30,31};\n\tint y = timestru.tm_year;\n\tint m = timestru.tm_mon;\n\tint d = timestru.tm_mday;\n\n\tif(y%4==0 &amp;&amp; y%100!=0 || y%400==0)\n\t\tx&#91;2]=29;\n\n\tif( m == 0)\n\t\treturn 0;\n\telse\n\t\tif(x&#91;m] == d)\n\t\t\treturn 1;\n\t\telse\n\t\t\treturn 0;\t\n}\n\nvoid strtodatetime(char *timeptr,struct tm *timestru)\n{\n\tchar yy&#91;4]={'\\0'},mm&#91;2]={'\\0'},day&#91;2]={'\\0'};\n\t\n\tmemset(yy,0,4);\n\twhile(*timeptr ==' ' || *timeptr=='\\t')\n\t\ttimeptr++;\n\tstrncpy(yy,timeptr,4);\n\ttimeptr+=5;\n\tstrncpy(mm,timeptr,2);\n\ttimeptr+=3;\n\tstrncpy(day,timeptr,2);\n\t\t\n\ttimestru->tm_year=atoi(yy);\n\ttimestru->tm_mon= atoi(mm);\n\ttimestru->tm_mday= atoi(day);\n\t\n}\n\nmy_bool months_between_init(UDF_INIT *initid,UDF_ARGS *args,char *message)\n{\n   if(args->arg_count != 2)\n   {\n     strcpy(message,\"months_between() must be three arguments\");\n\t return 1;\n   }\n\n   initid->extension=NULL;\n   initid->maybe_null=1;\n  \n   return 0;\n}\n\ndouble months_between(UDF_INIT *initid,UDF_ARGS *args,char *result,unsigned long *length,char *is_null,char *error)\n{\n\t\/\/FILE *fp;\n\t\/\/fp = fopen(\"\/home\/gbase\/udf\/oo\",\"w+\");\n\t\n    char v_first&#91;20]={'\\0'};\n    char v_second&#91;20]={'\\0'} ;\n    \n\tstruct tm first_tm;\n\tstruct tm second_tm;\n\t\n\tint v_first_end=0;\n\tint v_second_end =0;\n\tdouble vm_result=0;\n\t\n \tstrcpy(v_first,args->args&#91;0]);\n \tstrcpy(v_second,args->args&#91;1]);\n\t\/\/fprintf(fp,\" return 1 %s, %s \\n\",v_first, v_second);\n\t\/\/fflush(fp);\n\tstrtodatetime(v_first,&amp;first_tm);\n\tstrtodatetime(v_second,&amp;second_tm);\n\t\/\/fprintf(fp,\" return 2 %d, %d,%d \\n\",first_tm.tm_year, first_tm.tm_mon,first_tm.tm_mday);\n\t\/\/fprintf(fp,\" return 3 %d, %d,%d \\n\",second_tm.tm_year, second_tm.tm_mon,second_tm.tm_mday);\n\t\/\/fflush(fp);\n\t\n\tv_first_end= is_end(first_tm);\n\tv_second_end= is_end(second_tm);\t\n\t\/\/fprintf(fp,\" return 2 %d, %d \\n\",v_first_end,v_second_end);\n\t\/\/fflush(fp);\n\tif(v_first_end &amp;&amp; v_second_end)\n\t\tvm_result= (first_tm.tm_year-second_tm.tm_year)*12 +(first_tm.tm_mon-second_tm.tm_mon);\n\telse\n\t\tvm_result= (first_tm.tm_year-second_tm.tm_year)*12 +(first_tm.tm_mon-second_tm.tm_mon)+(first_tm.tm_mday-second_tm.tm_mday)\/31.0 ;\n\t\t\n\t\/\/fclose(fp);\n\treturn vm_result;\n\t\n}\n\nvoid months_between_deinit(UDF_INIT *initid)\n{\n\n}<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>\u6839\u636eOracle\u7684\u89c4\u5b9a\uff0cmonths_bwtween\u8fd4\u56de2\u4e2a\u65e5\u671f\u76f8\u5dee\u7684\u6708\u6570\uff0c\u672c\u6587\u4ecb\u7ecd\u5728GBase 8a\u6570\u636e\u5e93\u96c6\u7fa4\u91cc\u5982\u4f55\u5b9e\u73b0\u8be5\u529f\u80fd\u51fd\u6570\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[36],"class_list":["post-4788","post","type-post","status-publish","format-standard","hentry","category-gbase8a","tag-36"],"_links":{"self":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/4788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/comments?post=4788"}],"version-history":[{"count":10,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/4788\/revisions"}],"predecessor-version":[{"id":4802,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/4788\/revisions\/4802"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=4788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=4788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=4788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}