-1

I have different Year, Month and Day columns, And I wish to exclude weekends.

How can I do so?

Thank you very much!

  • 4
    Welcome to Database Administrators! Which DBMS are you using? "SQL" is just a query language, not the name of a specific database product. Please add the tag for the database product you are using, e.g. [sql-server], [postgresql], [oracle], [db2], ... – Glorfindel Sep 25 '18 at 8:24
  • 5
    What is a weekend for you? That word has different meaning in different areas of the world. – a_horse_with_no_name Sep 25 '18 at 8:45
2

The exact answer will depend on which SQL database product you're using, because they all support different functions for date manipulation. For example, here is a demo for this SQL Server query:

SELECT DATEFROMPARTS(year, month, day)
  FROM table1
  WHERE DATEPART(WEEKDAY, DATEFROMPARTS(year, month, day))
          NOT IN (1, 7) -- Sunday, Saturday

and here is one for MySQL:

SELECT DATE_ADD(DATE_ADD(MAKEDATE(year, 1),
                         INTERVAL (month)-1 MONTH),
                INTERVAL (day)-1 DAY)
  FROM table1
  WHERE WEEKDAY(DATE_ADD(DATE_ADD(MAKEDATE(year, 1),
                         INTERVAL (month)-1 MONTH),
                INTERVAL (day)-1 DAY))
          NOT IN (5, 6) -- Saturday, Sunday

Not the answer you're looking for? Browse other questions tagged or ask your own question.