NinethSense oWnZ mE!: Praveen’s drawing book

Choose a Topic:

Thu
22
Feb '07

T-SQL Find previous month

For a projet I had to find the previous month - in format ‘mm.yyyy’.

Here is the code:
– =============================================
– Author:		Praveen
– Create date: 22-02-2007
– Description:	To find the previous month in format ‘month.year’
– =============================================
 
CREATE FUNCTION dbo.gf_PreviousMonth (@d char(7))
RETURNS char(7) 
AS 
BEGIN 
	DECLARE @dt AS datetime
	SET              @dt = dateadd(year, (CONVERT(int, substring(@d, charindex(‘.’, @d, 0) + 1, 4))) - 2000, ‘1/1/2000′)
	SET              @dt = dateadd(month, (CONVERT(int, substring(@d, 1, charindex(‘.’, @d, 0) - 1))) - 1, @dt)
	SET              @dt = dateadd(month, - 1, @dt) 
	RETURN CONVERT(varchar(2), month(@dt)) + ‘.’ + CONVERT(varchar(4), year(@dt)) 
END
 
 
The CodeProject Microsoft Developer Network Official ASP.NET Forums Microsoft .NET Framework Community Microsoft Most Valuable Professional Kidoos forums Microsoft Visual Studio Developer Home Professional Information Technology Solutions Microsoft Research Home Kerala Microsoft Users Group Community Website