使用postgresql函数在两个日期之间获得结果

时间:2016-12-15 21:48:48

标签: postgresql postgresql-9.3

我在Firebird中有这个程序。此存储过程显示我们作为参数提供的两个日期之间的日期列表:

TypeError

我想将firebird程序翻译为 Postgresql

我的例子。我需要这样做:

CREATE PROCEDURE DIAS_ENTRE_FECHAS (
    first$date DATE,
    last$date DATE)
RETURNS (
    day$number INTEGEr,
    day$name CHAR(10),
    day$date DATE )
AS
DECLARE VARIABLE day$week INTEGER;
begin
  /* Procedure Text */

     day$date= FIRST$DATE;
     day$number = 0;

     while (day$date <= LAST$DATE) DO
     begin
          day$week=extract(weekday from day$date);

          IF (day$week=0) THEN
             day$name='DOMINGO';
          ELSE IF (day$week=1) THEN
             day$name='LUNES';
          ELSE IF (day$week=2) THEN
             day$name='MARTES';
          ELSE IF (day$week=3) THEN
             day$name='MIERCOLES';
          ELSE IF (day$week=4) THEN
             day$name='JUEVES';
          ELSE IF (day$week=5) THEN
             day$name='VIERNES';
          ELSE IF (day$week=6) THEN
             day$name='SABADO';

          day$number    = day$number+1;
          suspend;

          day$date = day$date+1;
     end

返回数据:

SELECT * FROM Dias_Entre_Fechas(start_date, end_date)

谢谢! ^^

1 个答案:

答案 0 :(得分:0)

你不需要一个功能,只需普通的SQL即可:

select row_number() over () as DAY$NUMBER,
       case extract( dow from d)
            when 0 then 'DOMINGO'
            when 1 then 'LUNES'
            when 2 then 'MARTES'
            when 3 then 'MIERCOLES'
            when 4 then 'JUEVES'
            when 5 then 'VIERNES'
            else 'SABADO'
        end as DAY$NAME,
        d as DAY$DATE
from generate_series( date '2016-02-02', date '2016-03-03', interval '1' day) d


day$number |day$name  |day$date            |
-----------|----------|--------------------|
1          |MARTES    |2016-02-02 00:00:00 |
2          |MIERCOLES |2016-02-03 00:00:00 |
3          |JUEVES    |2016-02-04 00:00:00 |
4          |VIERNES   |2016-02-05 00:00:00 |
5          |SABADO    |2016-02-06 00:00:00 |
6          |DOMINGO   |2016-02-07 00:00:00 |
7          |LUNES     |2016-02-08 00:00:00 |
8          |MARTES    |2016-02-09 00:00:00 |
9          |MIERCOLES |2016-02-10 00:00:00 |
10         |JUEVES    |2016-02-11 00:00:00 |
11         |VIERNES   |2016-02-12 00:00:00 |
12         |SABADO    |2016-02-13 00:00:00 |
13         |DOMINGO   |2016-02-14 00:00:00 |
14         |LUNES     |2016-02-15 00:00:00 |
15         |MARTES    |2016-02-16 00:00:00 |
16         |MIERCOLES |2016-02-17 00:00:00 |
17         |JUEVES    |2016-02-18 00:00:00 |
18         |VIERNES   |2016-02-19 00:00:00 |
19         |SABADO    |2016-02-20 00:00:00 |
20         |DOMINGO   |2016-02-21 00:00:00 |
21         |LUNES     |2016-02-22 00:00:00 |
22         |MARTES    |2016-02-23 00:00:00 |
23         |MIERCOLES |2016-02-24 00:00:00 |
24         |JUEVES    |2016-02-25 00:00:00 |
25         |VIERNES   |2016-02-26 00:00:00 |
26         |SABADO    |2016-02-27 00:00:00 |
27         |DOMINGO   |2016-02-28 00:00:00 |
28         |LUNES     |2016-02-29 00:00:00 |
29         |MARTES    |2016-03-01 00:00:00 |
30         |MIERCOLES |2016-03-02 00:00:00 |
31         |JUEVES    |2016-03-03 00:00:00 |

你说:

  

例如:
SELECT * FROM Dias_Entre_Fechas(start_date,end_date)
  返回数据:DAY $ NUMBER DAY $ NAME DAY $ DATE
  --------------------------------------
1 MARTES 12/01/2010 < br /> 2 MIERCOLES 13/01/2010
3 JUEVES 14/01/2010
4 VIERNES 15/01/2010

只是做:

select row_number() over () as DAY$NUMBER,
       case extract( dow from d)
            when 0 then 'DOMINGO'
            when 1 then 'LUNES'
            when 2 then 'MARTES'
            when 3 then 'MIERCOLES'
            when 4 then 'JUEVES'
            when 5 then 'VIERNES'
            else 'SABADO'
        end as DAY$NAME,
        d::date as DAY$DATE
from generate_series( date '2010-01-12', date'2010-01-15', interval '1' day) d
;

day$number |day$name  |day$date   |
-----------|----------|-----------|
1          |MARTES    |2010-01-12 |
2          |MIERCOLES |2010-01-13 |
3          |JUEVES    |2010-01-14 |
4          |VIERNES   |2010-01-15 |