Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
alter function fn(@mdvn int, @fcdate datetime, @tcdate datetime)
returns @t1 table
(ANC int /*,TT1 int, TT2 int*/)
as
begin
declare @ANC int,@TT1 int,@TT2 int
declare @fdate datetime
declare @tdate datetime
set @fdate=convert(varchar(20),@fcdate,103)
set @tdate=convert(varchar(20),@tcdate,103)
declare ancur cursor for
select sum(ANC1) as ANC,SUM(TT1) as TT1,sum(TTB2) as TT2 from
(
select dvn_cd,
	case when visit_no=3 and Visit_date between @fdate and @tdate then 1 else 0 end as ANC1,
	case when TTB=1 and TTDate between @fdate and @tdate then 1 else 0 end as TT1,
	case when TTB>1 and TTDate between @fdate and @tdate then 1 else 0 end as TTB2 
from anvisits3 where dvn_cd=@mdvn and ANEDD between @fdate and @tdate
)a group by dvn_cd
open ancur
fetch next from ancur into @ANC,@TT1,@TT2
while @@fetch_status=0
begin
	insert into @t1
	select 'ANC'+convert(varchar(20),@ANC)+',TT1'+convert(varchar(20),@TT1)+',TT2'+convert(varchar(20),@TT2)
	fetch next from ancur into @ANC,@TT1,@TT2
end
return
end


---------------

SQL
select * from fn(41,'01/01/2011','2011/01/31')

Msg 242, Level 16, State 3, Line 1<br />
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.



------------------
my query was executed successfully.. but i dint get my result
Posted
Updated 8-Jan-14 2:23am
v2

the problem is in your calling to the function...

SQL
select * from fn(41,'01/01/2011','2011/01/31')


date you have passed '01/01/2011' is successfully converted to datetime

SQL
SELECT convert(datetime,'01/01/2011')
--result is 
---2011-01-01 00:00:00.000


but the date '2011/01/31' is not able to converted to datetime
SQL
SELECT convert(datetime,'2011/01/31')
--result
--Msg 242, Level 16, State 3, Line 1
--The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.



for getting resolve your issue your need to call your funcation like this...
SQL
select * from fn(41,'01/01/2011','01/31/2011')---MM/dd/yyyy format


to know more about datetime conversion and its formats please review the link..

http://www.sqlusa.com/bestpractices/datetimeconversion/[^]
 
Share this answer
 
Have a look at your code:
SQL
alter function fn(@mdvn int, @fcdate datetime, @tcdate datetime)
--input parameters are datetime data type
--...
--below 2 lines are unnecessary, because datetime is datetime and don't need to be converted to varchar!
--declare @fdate datetime
--declare @tdate datetime
--set @fdate=convert(varchar(20),@fcdate,103) -- error here!
--set @tdate=convert(varchar(20),@tcdate,103) -- error here!

Use input parameters!


SQL
select * from fn(41,'01/01/2011','2011/01/31')

Be consistent about passing parameters! Once it is mdy format, another time is ymd format.

See SET DATEFORMAT[^] documentation for further information.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900