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

VN:F [1.1.6_502]
Rating: 0.0/5 (0 votes cast)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Related posts:

  1. Find the Quarter which a month belongs
  2. Build DateTime from values
  3. Winforms Calendar code – C#
  4. SQL – Convert columns to a row (concatenation)
  5. Reverse text as you type