计算oracle中两个日期之间的工作日

时间:2017-10-24 11:18:09

标签: sql oracle

以下是我的功能,我试图计算两个日期之间的工作日数。

CREATE OR REPLACE function workingdays 
( start_date IN DATE , end_date IN DATE)

return  number

as

total_days varchar2(10);

v_count integer:= 0;

end_value date := to_date(end_date , 'YYYY-MM-DD');

start_value date := to_date(start_date , 'YYYY-MM-DD');

date_diff number(10);


begin

while(start_value <= end_value)

loop

        if (to_char(TO_DATE(start_value,'YYYY-MM-DD') , 'D') = 1 or to_char(TO_DATE(start_value,'YYYY-MM-DD'), 'D') = 7)
        then v_count := v_count+1;
        end if;

start_value := start_value + 1;

end loop;

date_diff :=to_number( to_date( end_date, 'YYYY-MM-DD') - to_date (start_date , 'YYYY-MM-DD')) ;

total_days := to_char( (to_number(date_diff) - to_number(v_count)) + 1);


return (' The total working days is' || to_number((total_days)));


end;
/

函数编译成功,但在执行时,我在返回行收到错误消息。有人可以指导我。这是数字/字符转换的一些问题。

2 个答案:

答案 0 :(得分:1)

你做了很多无用的转换,几乎所有的转换都可以跳过。

TO_CHAR(..., 'D')的结果取决于当前用户NLS_TERRITORY值,因此除非您确保设置NLS_TERRITORY,否则不应使用它。

这个正在运作:

CREATE OR REPLACE FUNCTION workingdays(start_date IN DATE , end_date IN DATE) RETURN VARCHAR2 AS

    total_days NUMBER;
    v_count INTEGER:= 0;
    end_value DATE := TRUNC(end_date);
    start_value DATE := TRUNC(start_date);
    date_diff NUMBER;

BEGIN

    WHILE start_value <= end_value LOOP
        IF TO_CHAR(start_value, 'fmDay', 'NLS_DATE_LANGUAGE = american') IN ('Saturday','Sunday') THEN 
            v_count := v_count + 1;
      END IF;
      start_value := start_value + 1;
    END LOOP;
    date_diff := end_value - TRUNC(start_date);
    total_days := date_diff - v_count + 1;

    RETURN ' The total working days is ' || total_days;

END;
/

还有许多其他可能性,这只是另一种方式:

CREATE OR REPLACE FUNCTION workingdays(start_date IN DATE, end_date IN DATE) RETURN VARCHAR2 AS
    next_run_date DATE := TRUNC(start_date);
    total_days INTEGER := 0;
BEGIN
    LOOP
        DBMS_SCHEDULER.EVALUATE_CALENDAR_STRING('FREQ=DAILY;INTERVAL=1;BYDAY=MON,TUE,WED,THU,FRI', NULL, next_run_date, next_run_date);
        EXIT WHEN next_run_date >= end_date;
        total_days := total_days + 1;
    END LOOP;
    RETURN ' The total working days is ' || total_days;
END;

答案 1 :(得分:0)

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

   <context:component-scan base-package = "com.acnovate" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>