2013-03-04 20 views
6

Sto lavorando su un database di Postgres 8.3. Una query che utilizzo è intesa per selezionare solo le righe incluse nei giorni feriali. In questo momento devo farlo a mano come nell'esempio qui sotto, ma voglio trasferirlo in qualche modo in cui posso specificare le date di inizio e fine e ottenere la stessa logica da applicare come di seguito. Questo èScrittura di una funzione per selezionare i dati solo nei giorni feriali in PostgreSQL

Come è possibile creare una funzione il cui ingresso è una data di inizio e di fine e il risultato della funzione sarà quello di selezionare tutte le righe inclusi solo nei giorni feriali di set di dati (voglio exlude ogni Staurday e domenica come nella clausola where in basso)?

create table filter_tbl as 
select * 
from base_tbl where 
(start_Time >= '2012-11-5' and start_Time < '2012-11-10') 
or (start_time >= '2012-11-12' and start_time < '2012-11-17') 
or (start_time >= '2012-11-19' and start_time < '2012-11-24') 
or (start_time >= '2012-11-26' and start_time < '2012-12-01') 
or (start_time >= '2012-12-03' and start_time < '2012-12-07') 
or (start_time >= '2012-12-10' and start_time < '2012-12-14') 
or (start_time >= '2012-12-17' and start_time < '2012-12-21') 
or (start_time >= '2012-12-24' and start_time < '2012-12-28') 
or (start_time >= '2012-12-31' and start_time < '2013-01-04') 
or (start_time >= '2013-01-07' and start_time < '2013-01-11') 
or (start_time >= '2013-01-14' and start_time < '2013-01-18') 
or (start_time >= '2013-01-21' and start_time < '2013-01-25') 
or (start_time >= '2013-01-28' and start_time < '2013-02-02') 
or (start_time >= '2013-02-04' and start_time < '2013-02-09') 
or (start_time >= '2013-02-11' and start_time < '2013-02-16') 
or (start_time >= '2013-02-18' and start_time < '2013-02-23') 
or (start_time >= '2013-02-25' and start_time < '2013-03-02') 
or (start_time >= '2013-03-04' and start_time < '2013-03-09') 
or (start_time >= '2013-03-11' and start_time < '2013-03-16'); 
+1

Si dovrebbe davvero considerare l'aggiornamento. 8.3 non è più supportato. –

+0

Qual è il tipo di dati di 'start_Time'? –

risposta

17

In base a tuo esempio sembra che start_time è il testo. Quindi è necessario convertirlo in timestamp utilizzando to_timestamp e quindi estrarre il giorno della settimana utilizzando EXTRACT.

tuo clausola WHERE sarà simile a questo:

WHERE EXTRACT(dow FROM timestamp (to_timestamp(start_time, "YYYY-MM-DD")) 
NOT IN (0,6) 

vicini: Data Type Formatting Functions e Date/Time Functions and Operators.

1

Controllare il datetime functions

esempio

SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'); 

rendimenti 5, che rappresenta Friday.

1

Quanto segue deve restituire il giorno della settimana.

date_part('dow', Date); 

Con le impostazioni di default 0 è Domenica e Sabato 6 è

4
select * 
from base_tbl 
where extract(dow from start_time) in (1,2,3,4,5) 
1
select to_char(date, 'Day') from table 
+0

Questo è per Oracle SQL – karz

Problemi correlati