Solved

SQL Server - Function Error for SELECT - RETURN Value

Posted on 2013-01-02
1
454 Views
Last Modified: 2013-01-02
Hi ....

Here is a Function I want to build in order to return me one value which is a DateTime format ...

I got this error any Time I tried to execute it :

Msg 444, Level 16, State 3, Procedure DateInterval, Line 20
Select statements included within a function cannot return data to a client

I will make a Stored Procedure as a last alternative, but I would like to know if this is related to the fact that I am using a SELECT to get my Return Value ? Make no sense for me

Any help


ALTER FUNCTION [dbo].[DateInterval] (@Month Int, @Year Int, @Interval Int)

RETURNS DateTime  

AS

BEGIN

      DECLARE @MyDate DateTime
      DECLARE @EOMonthDate Datetime
      DECLARE @DateInterval Datetime

      -- Get a date from the two Inputs @Month & @Year
      SET @MyDate = CONVERT(DateTime, CONVERT(VARCHAR(4), @Year) + RIGHT('0' + CONVERT(VARCHAR(2), @Month), 2) + '01', 102)
      SELECT CONVERT(VARCHAR(10),@MyDate ,105)

      -- Get the latest Day-Month from the built Date
      SET @EOMonthDate = (SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, @MyDate) +1, 0)))

      -- Return the Date after calculation  
      SET @DateInterval = (SELECT DATEADD(MM, @Interval, @EOMonthDate ))

      -- Return the result of the function
      RETURN @DateInterval
      
END
0
Comment
Question by:venmarces
1 Comment
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 38738647
The error is that you have this statement in the function where the output is not assigned to a variable

SELECT CONVERT(VARCHAR(10),@MyDate ,105)

The default behavior is to output a select statement like that to the client.  In a function it can't do that.

So assign it to a variable if it's important, or remove it if it's not.

(Tested on SQL Server 2005, removing the line made it compile - but if you need it, I suggest assigning it to a variable and using it for whatever you need it for).
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now