[RESOLVED]Function to find the months name between two dates – sql server

Hi, I want to find out month names between two dates for which I have done a function.

I cannot execute the function (

error : 

Msg 178, Level 15, State 1, Procedure GetnMonths, Line 23
A RETURN statement with a return value cannot be used in this context.
Msg 102, Level 15, State 31, Procedure GetnMonths, Line 27
Incorrect syntax near ‘BEGIN’.)

is coming. Pl find the function given below.

– exec GetnMonths (’2014-09-01′,’2014-11-03′)
IF OBJECT_ID(‘GetnMonths’,’FN’) IS NOT NULL
DROP FUNCTION GetnMonths;
GO
CREATE FUNCTION GetnMonths
(
@startdate DATE,
@endate DATE
)
RETURNS TABLE
AS
BEGIN
DECLARE @yy varchar(4) = ’2014′
DECLARE @sql VARCHAR(2000)
BEGIN

SET @sql = ‘SELECT a.months+”’ – ”’+a.myear [mmyyyy]
FROM (
VALUES
(1,”January”,@yy),(2,”February”,@yy),(3,”March”,@yy),(4,”April”,@yy),
(5,”May”,@yy),(6,”June”,@yy),(7,”July”,@yy),(8,”August”,@yy),
(9,”September”,@yy),(10,”October”,@yy),(11,”November”,@yy),(12,”December”,@yy)
) a(number,months,myear)
WHERE a.number >= DATEPART(month,@startdate)
AND a.number <= DATEPART(month,@endate)’
exec( @sql)
return mmyyyy
END

END
GO

Pl suggest what corrections to be done. Thanks in advance.

remove the second begin and end from your procedure

Hi Uma,

For current forum is related with html,css,javascript  ,while your issue is related with sql  function, i suggest that you can post it to the link below for a professional solution:

http://forums.asp.net/1226.aspx/1?SQL+Server+SQL+Server+Express+and+SQL+Compact+Edition

Best Regards,

Kevin Shen.

Leave a Reply