SQL

Sql code posted by ff
created at 24 Nov 19:15, updated at 26 Nov 10:21

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

CREATE OR REPLACE FUNCTION COMPARE_DATE(p_idSociete in VARCHAR2, dateTrait in DATE, dateComp in DATE)
RETURN number -- return 1 if dateTrait (or inferior) equal dateComp
IS
------------------------------------------------
-- $Revision: 1.7 $ - @version.fullname@
------------------------------------------------
/*---------------------------------------------------------------------------------------------------

   Function: COMPARE_DATE

     Compare if two date are equals or inferior for a selected portfolio company (ie WE and public holidays)

   Parameters:

     p_idsociete  - portfolio company identifier (mandatory)
     dateTrait    - date of traitment 
     datedomp     - comparison date 

   Returns:

      1 if datetrait is equal or inferior to datecomp, 0 otherwize  (NUMBER)
   
---------------------------------------------------------------------------------------------------*/

dateTraitLoop Date; -- date of traitment that can down in day in order to take saturday, sunday or a public holiday 
dateJNO Date; -- date of public holiday
i number := 0; -- increment
pCTX plog.log_ctx := plog.init( 'SID'||sys_context('USERENV', 'SID') );
BEGIN
  
  plog.setBeginSection(pCTX, 'COMPARE_DATE');
  -- for debug purpose remove comment at the beginning of the next line
  --plog.setLevel (pCTX, PLOG.LDEBUG);

-- Treatment of the current day
  IF (trunc(dateTrait, 'DD') = dateComp) THEN RETURN 1;

  ELSE
-- Treatment of saturday, sunday, or public holiday
    LOOP
       i := i + 1;
       dateTraitLoop := dateTrait - i;
       plog.debug(pCTX,' treatment date:  ' ||  dateTraitLoop);
       plog.debug(pCTX,'treatment date day:  ' ||  to_char(dateTraitLoop, 'DAY'));
       
       -- Do comparaison with saturday or sunday
       IF (
             (to_char(dateTraitLoop, 'DY') = 'SAT') OR  -- saturday found
           (to_char(dateTraitLoop, 'DY') = 'SUN')) THEN  -- sunday found

                   plog.debug(pCTX,'saturday, sunday, Comparaison between:  ' ||  dateTraitLoop);
            plog.debug(pCTX,'and:  ' ||  dateComp);
            IF (trunc(dateTraitLoop, 'DD') = dateComp) THEN RETURN 1; -- comparaison between saturday or sunday
            END IF;
            
       ELSE
       -- Do comparaison with public holiday
           select jno.DT_JNO into dateJNO from jno where to_char(jno.DT_JNO,'DD/MM/YYYY') = to_char(dateTraitLoop, 'DD/MM/YYYY') and jno.ID_SOCIETE = p_idSociete;
          IF (dateJNO is not null) THEN 
                  plog.debug(pCTX,'public holiday, Comparaison between:  ' ||  dateTraitLoop);
               plog.debug(pCTX,'and:  ' ||  dateComp);
               IF (trunc(dateJNO, 'DD') = dateComp) THEN RETURN 1; -- comparaison with a public holiday
               END IF;
          ELSE RETURN 0;
          END IF;
      END IF;
     END LOOP;
     END IF;

     plog.setendsection(pCTX, 'COMPARE_DATE');
   RETURN 0;
END COMPARE_DATE;
/
2.79 KB in 10 ms with coderay