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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Function to find whether last days of the month is

Author  Topic 

sandesh.ravi
Posting Yak Master

110 Posts

Posted - 2012-03-08 : 11:41:30
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 date
AS
BEGIN

DECLARE @PrevMonthDate date

SET @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)
END

RETURN @PrevMonthDate

END

and call it like

SELECT dbo.GetLastMonthWeekDay('2011-12-31')
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 DATE
AS
BEGIN
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;
END
GO
Use as
SELECT dbo.GetLastMonthWeekDay ('20111231')
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-03-08 : 16:14:08
I betcha it's one of 4 numbers

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2012-03-09 : 07:45:48
What if monday is a holiday?

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

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?



PARTY!

The ay after the superbowl should be a holiday

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

sandesh.ravi
Posting Yak Master

110 Posts

Posted - 2012-03-09 : 11:04:46
Thank you for the help.

Thanks,
Sandesh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-09 : 11:38:02
quote:
Originally posted by DonAtWork

What if monday is a holiday?

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -