Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi,I am looking for a function to identify whether last day of the previous month is weekday or a weekend.If it is a weekend then it should return the next weekday.For example. 31/12/2011 was saturday.The function should return 02/01/2012 as it was the next working day(Monday).If it was a weekday then it should return the date.Kindly let me know the function.Thanks,Sandesh
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2012-03-08 : 12:33:05
[code]CREATE FUNCTION GetLastMonthWeekDay(@Date date)RETURNS dateASBEGINDECLARE @PrevMonthDate dateSET @PrevMonthDate=CASE DATENAME(dw,DATEADD(mm,DATEDIFF(mm,0,@Date)-1,@Date)) WHEN 'Saturday' THEN DATEADD(mm,DATEDIFF(mm,0,@Date)-1,@Date)+2 WHEN 'Sunday' THEN DATEADD(mm,DATEDIFF(mm,0,@Date)-1,@Date)+1 ELSE DATEADD(mm,DATEDIFF(mm,0,@Date)-1,@Date) ENDRETURN @PrevMonthDateENDand call it likeSELECT dbo.GetLastMonthWeekDay('2011-12-31')[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2012-03-08 : 15:12:15
This returns the next business date(M-F) if the date passed in is a weekend. Otherwise returns the date itself.
CREATE FUNCTION dbo.GetLastMonthWeekDay (@dt date)RETURNS DATEASBEGIN DECLARE @weekday DATE = DATEADD( dd, CASE WHEN DATEDIFF(dd, '19000101', @dt)%7 >= 5 THEN 7 -DATEDIFF(dd, '19000101', @dt)%7 ELSE 0 END, @dt ); RETURN @weekday;ENDGO
What if monday is a holiday? http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp
X002548
Not Just a Number
15586 Posts
Posted - 2012-03-09 : 09:32:23
quote:Originally posted by DonAtWork What if monday is a holiday?
quote:Originally posted by DonAtWork What if monday is a holiday? http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp
still it wont be the weekend right? it would be an extended weekend ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/