4/4/11

SQL - Function - Month Name

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Fn_MonthName]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[Fn_MonthName]
go
Create function Fn_MonthName (@MonthCode varchar(2))
Returns Varchar (15)
As
/*------------------------------------------------------------------------------------------------------------------------------------------
** SQLVersion : SQL 2000
** Function    : Fn_FinMonth
** Author     : Kartik M Kumar
** DateTime    : 29 January 2011 21:27
** Version    : 1.1
** Purpose    : To get the Month Name from the respective calander month
** ToCheck    :
** Changes    :
------------------------------------------------------------------------------------------------------------------------------------------*/
/*
-- Start of Debugging Stuff
    Declare @MonthCode varchar(2)
    Set @MonthCode = '03' -- Pass the Month Number as Input
-- End of Debugging Stuff
*/
Begin
    Declare @MonthName varchar(15)
    DECLARE @IMonthCd as int
    set @IMonthCd = cast(@MonthCode as Int)
    Select @MonthName = case @IMonthCd
        when 1 then 'January'
        when 2 then 'Feburary'
        when 3 then 'March'
        when 4 then 'April'
        when 5 then 'May'
        when 6 then 'June'
        when 7 then 'July'
        when 8 then 'August'
        when 9 then 'September'
        when 10 then 'October'
        when 11 then 'November'
        when 12 then 'December'
                end
Return Isnull(@MonthName, 'InvalidMonth')
end
go

http://www.sqlservercentral.com/scripts/Month+Name/72408/

No comments: