Write a Function to get Character Month in Sql Server 2005?

Here is the sample code to get character month.
Input is any number between 1 to 12
Answer will be 'January' for 1... 'December' for 12.

How to get Character Month?

CREATE FUNCTION [getCharacterMonth](@month int)
RETURNS varchar(10)
as
begin
declare @cmonth varchar(10)
if (@month = 1) set @cmonth = 'January'
else if (@month = 2) set @cmonth = 'February'
else if (@month = 3) set @cmonth = 'March'
else if (@month = 4) set @cmonth = 'April'
else if (@month = 5) set @cmonth = 'May'
else if (@month = 6) set @cmonth = 'June'
else if (@month = 7) set @cmonth = 'July'
else if (@month = 8) set @cmonth = 'August'
else if (@month = 9) set @cmonth = 'September'
else if (@month = 10)set @cmonth = 'October'
else if (@month = 11) set @cmonth = 'November'
else if (@month = 12)set @cmonth = 'December'
return @cmonth
end

To Run this Function
print dbo.getCharacterMonth ( 1 )



As Per Mr. Mitesh Oswal's comment I modified the Coding..

DECLARE @intMonth int
SET @intMOnth = 2
select DateName(Month,convert(varchar(2),@intMonth) + '/01/2000') -- 'February'

Thanks to Mitesh Oswal

2 comments:

Mitesh Oswal said...

Without Using If Else Condition we can use

DECLARE @inMonth VARCHAR(10)
SET
@inMOnth = '02'

SELECT Datename(MOnth,@inMOnth+'/01/'+'2000')

Sharepoint the Great said...

Thanks Dear Mitesh Oswal.