-1

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

How can I do so?

Thank you very much!

Paul White
  • 83,961
  • 28
  • 402
  • 634
BitTheBit
  • 1
  • 1
  • 1
  • 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 08:24
  • 5
    What is a weekend for you? That word has different meaning in different areas of the world. –  Sep 25 '18 at 08:45

1 Answers1

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
Glorfindel
  • 2,201
  • 5
  • 17
  • 26