Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Name	        EnterDate	   todate
Shireeshab	12/15/2011	12/16/2011
anila	       12/16/2011	12/16/2011
ravichand	12/12/2011	12/13/2011
manoj	       1/4/2013	        3/4/2013
raju	      12/15/2011	12/16/2011
Srinivas       1/6/2012	        1/6/2012

I have the data like this. now i want to get beetween dates from enterdate and todate
for example

for manoj enter date 1/4/2013 and todate is 3/4/2013 so i want to get 1/4/2013,2/4/2013,3/4/2013 in one column
like that all the names.

Desired output is
12/15/2011
12/16/2011
 12/16/2011
12/12/2011
12/13/2011
1/4/2013
2/4/2013
3/4/2013
Posted
Updated 27-May-13 0:40am
v2

Try this:
SQL
create procedure EnumerateDates 
@fromdate datetime, 
@todate datetime
as
begin
with mycte as
(
	select @fromdate DateValue
    union all
    select DateValue + 1
    from    mycte   
    where   DateValue + 1 <= @todate
)
select DateValue
from    mycte
OPTION (MAXRECURSION 0)
end


Usage:
SQL
exec EnumerateDates '2013.01.01','2013.01.10'
 
Share this answer
 
Comments
damodara naidu betha 19-Jun-13 1:30am    
Good one. 5+
Hi prakash.chakrala,

you can try this,


create FUNCTION Dates(@fromdate datetime,@todate datetime)
returns varchar(1000) 
as
begin

	DECLARE @Count INT
	DECLARE @Len INT
	Declare @dates varchar(max)

	set @count=1;
	set @Len= DATEDIFF(Day,@fromdate,@todate)
	set @dates=CONVERT(Varchar,@fromdate,105)

	  while @Count<=@Len
	  begin
			set @fromdate=@fromdate+1;
			set @dates=@dates+''','+CONVERT(varchar,@fromdate,105);
			set @Count=@Count+1;
	 end
	 return @dates
end


SQL
select Name,Dates(EnterDate,ToDate)
from table1 (nolock)
where name='manoj'



Hope it will help you...

Happy Coding
 
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