If a function return a table then that function is called table value function
create function fxemp1(@sd datetime,@ed datetime)returns @tb table(cdate datetime,weekdate varchar(50))
as
begin
declare @s varchar(50)
while (@ed>=@sd)
begin
if(datepart(dw,@sd))=1
set @s='S'
else if(datepart(dw,@sd))=2
set @s='M'
else if(datepart(dw,@sd))=3
set @s='T'
else if(datepart(dw,@sd))=4
set @s='W'
else if(datepart(dw,@sd))=5
set @s='Th'
else if(datepart(dw,@sd))=6
set @s='F'
else if(datepart(dw,@sd))=7
set @s='St'
set @s=convert(varchar(2),day(@sd))+' '+@s
insert into @tb values(@sd,@s)
set @sd=dateadd(day,1,@sd)
end
return
end
execute the function
select * from dbo.fxemp1('2012-jan-1','2012-jan-31')
No comments:
Post a Comment