How to select only the days of the week?

2

How do I get weekdays, excluding weekends, using SQL?

I need to notify the customer about some debt, and if it is Friday I can not charge it because it will start the weekend, it will take Saturday and Sunday to pay so it will only be notified on Monday if you did not make the payment.

SELECT P.dtvenc, 
FROM foo P
WHERE p.dtvenc = To_date(SYSDATE)-1
    
asked by anonymous 03.11.2014 / 18:29

1 answer

5

Depends on the DBMS you use.

If it is SQL Server, use the DATEPART function. I will leave it at your discretion to read the documentation on the link. The form would be as follows:

WHERE DATEPART(p.dtvenc, 'weekday') BETWEEN 2 AND 6

Where 2 and Monday and 6 is friday.

If you are an Oracle, you use the form to_char . which receives a date.

WHERE to_char(p.dtvenc, 'D') in ('2', '3', '4', '5', '6')

Editing: @ Caffé drew attention to the fact that the number corresponding to each day of the week in Oracle may vary. This depends on the language / culture set up. For more details (if you are curious or have problems with numbers), I suggest searching for National Language Support Caffé commented).

If it's MySQL, PostgreSQL or another system ... In this case you expect someone who knows them to respond here. Putting the bank tag you use in the question helps you get better answers.

    
03.11.2014 / 18:46