Skip to content
Advertisement

How to select every Friday for the last six months?

I am working on the currency table. I need to get every Friday for the last six months. My query selects every day between two dates. Here my query

SELECT valid_from,
       currency_code,
       currency_rate
 FROM currency_rate
 WHERE valid_from > add_months(sysdate,-6) --Selects every day
 AND currency_code IN ('USD', 'EUR','CAD','GBP','JPY') 
 AND currency_type IN ('MBDS')
 ORDER BY currency_code,valid_from;

OUTPUT

1   23/07/2019  CAD 4.3561
2   24/07/2019  CAD 4.3368
3   25/07/2019  CAD 4.3636
4   26/07/2019  CAD 4.3539
5   27/07/2019  CAD 4.3059
6   28/07/2019  CAD 4.3059
7   29/07/2019  CAD 4.3059
8   30/07/2019  CAD 4.2849
9   31/07/2019  CAD 4.2429
10  01/08/2019  CAD 4.214
..  ../../....  CAD ......
..  ../../....  CAD ......
..  ../../....  CAD ......
173 21/01/2020  CAD 4.5281

OUTPUT(Should be)

1   26/07/2019  CAD 4.3561
2   02/08/2019  CAD 4.3368
3   09/08/2019  CAD 4.3636
4   16/08/2019  CAD 4.3539
5   23/08/2019  CAD 4.3059
6   ..........  CAD ......
7   ..........  CAD ......
..  ..........  CAD ......
..  ..........  CAD ......
..  10/01/2020  CAD ......
..  17/01/2020  CAD ......

Thanks for helping.

Advertisement

Answer

Not clear if you are looking for Oracle or MS SQL-Server. In case you need it for Oracle you can use

WHERE valid_from > add_months(sysdate,-6)
   AND TO_CHAR(valid_from, 'Dy', 'nls_date_language = american') = 'Fri'
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement