## how do i find 5th working day in a month.

 Author Message kishorefeb28 SSC Rookie Group: General Forum Members Points: 29 Visits: 96 Hi Experts,i would like to know how to find fifth workingday* of a given month and year.workingday - working day is just any day apart from saturday and sunday. no need to consider any other holidays.ex: I would just give month number and year as input and i would like to know date and day of the fifth working day.Input : 10/2013Output : 7th October 2013, Monday.any help would be appreciated.Thank youKishore. Koen Verbeeck SSCoach Group: General Forum Members Points: 16507 Visits: 13207 `SET DATEFIRST 1; -- first day of the week is a MondayDECLARE @month INT = 10;DECLARE @year INT = 2013;-- Tally table of 7 rowsWITH CTE_Tally AS( SELECT 0 AS Number UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6)SELECT resultdate = datesFROM ( SELECT dates, RID = ROW_NUMBER() OVER (ORDER BY dates) FROM ( SELECT dates = DATEADD(dd,Number,DATEFROMPARTS(@year,@month,1)) FROM CTE_Tally ) tmp WHERE DATEPART(weekday,dates) NOT IN (6,7) -- filter out saturday and sunday ) tmp2WHERE RID = 5;`I used the function DATEFROMPARTS, which is only available from SQL Server 2012. Since you posted this question in a SQL 2014 forum, I assume that won't be a problem. How to post forum questions.Need an answer? No, you need a question.What’s the deal with Excel & SSIS?Member of LinkedIn. My blog at SQLKover. MCSA SQL Server 2012 - MCSE Business Intelligence kishorefeb28 SSC Rookie Group: General Forum Members Points: 29 Visits: 96 Thanks a ton Koen.it worked. :-) Jeff Moden SSC-Forever Group: General Forum Members Points: 45267 Visits: 39934 As much as I love Tally Tables and as high performing as they can be, I find that pure math will frequently beat it (although I've not tested it in this case). This includes a "short circuit" for Tuesday thru Saturday (since we're not considering holidays, the 5th workday is ALWAYS the 7th of the month if the month starts on any of those days). The FROM clause in the following is the good ol' fashioned, backwards compatible way of converting separate INTs for Month and Year to a DATETIME for the first of that month. The number 22801 is 1900*12 months + 1 to get rid of the current month.I've also made it so the code is not dependent on the value of DATEFIRST so that this can easily be incorporated into a high performance iSF (Inline Scalar Function which is really an Inline Table Valued Function that returns a single value).`DECLARE @pMonth INT ,@pYear INT; SELECT @pMonth = 11 ,@pYear = 2013; SELECT FifthWeekDay = DATEADD(dd, CASE WHEN DATEDIFF(dd,-1,ca.FirstOfMonth)%7 > 1 -- -1 is a Sunday THEN 7 ELSE 6-DATEDIFF(dd,-1,ca.FirstOfMonth)%7 -- -1 is a Sunday END ,ca.FirstOfMonth-1) FROM (SELECT DATEADD(mm,@pYear*12-22801+@pMonth,0))ca(FirstOfMonth);`Someone with more caffeine in their system might be able to simplify this even more. :-D --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.Although change is inevitable, change for the better is not.Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:How to post code problemsHow to post performance problemsForum FAQs Jeff Moden SSC-Forever Group: General Forum Members Points: 45267 Visits: 39934 If you only need compatibility back to 2005, then I guess you could call the following a "simplification".`DECLARE @pMonth INT ,@pYear INT ,@Date DATETIME; SELECT @pMonth = 11 ,@pYear = 2013; SELECT FifthWeekDay = DATEADD(dd, CASE WHEN d.DoW > 1 THEN 7 ELSE 6-d.DoW END, f.FirstOfMonth-1) FROM (SELECT DATEADD(mm,@pYear*12-22801+@pMonth,0)) f (FirstOfMonth) CROSS APPLY (SELECT DATEDIFF(dd,-1,f.FirstOfMonth)%7) d (DoW);` --Jeff ModenRBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.Although change is inevitable, change for the better is not.Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:How to post code problemsHow to post performance problemsForum FAQs

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 14 - Administration      SQL Server 14 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      Question of the Day (QOD)      SQL Server Security Skills Microsoft Access      Microsoft Access Products and Books      Third Party Products         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         SQLCentric         Golden Gate Software         Idera         Red Gate Software         Lumigent         Quest Software         ApexSQL         CA      Discussions about Books         Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books         Discuss Data Warehousing Books          Discuss T-SQL Books          Discuss Programming Books          Discuss XML Books  Notification Services      Administration Article Discussions Future Versions      SQL 12