I have different Year, Month and Day columns, And I wish to exclude weekends.
How can I do so?
Thank you very much!
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityI have different Year, Month and Day columns, And I wish to exclude weekends.
How can I do so?
Thank you very much!
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