Click here to Skip to main content
15,888,218 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi all,
SQL
create table #leave 
( 
        LeaveId INT IDENTITY(1,1) 
        ,EmployeeId INT 
        ,FromDate Datetime 
        ,ToDate Datetime 
        ,[Status] VARCHAR(50) 
) 
 
insert into #leave values(1,'2011-10-25','2011-10-27','Approved') 
insert into #leave values(2,'2011-10-26','2011-10-27','Approved') 
insert into #leave values(3,'2011-10-27','2011-10-27','Rejected') 
insert into #leave values(1,'2011-11-01','2011-10-01','Approved') 
 
 
select * from #leave 


can anyone suggest me the query to return table in this way
EmployeeId       Date                        Status
-----------     -----------------------      --------------
1                 2011-10-25 00:00:00.000  Approved
1                 2011-10-26 00:00:00.000  Approved
1                 2011-10-27 00:00:00.000  Approved
2                 2011-10-26 00:00:00.000  Approved
2                 2011-10-27 00:00:00.000  Approved
3                 2011-10-27 00:00:00.000  Rejected
1                 2011-11-01 00:00:00.000  Approved

the date between FromDate and ToDate are also diplayed with EmployeeId and Status
thanks in advance
Posted
Updated 29-Nov-11 21:36pm
v2

TRY WITH THESE QUERIES..

SQL
create table #leave
(
        LeaveId INT IDENTITY(1,1)
        ,EmployeeId INT
        ,FromDate Datetime
        ,ToDate Datetime
        ,[Status] VARCHAR(50)
)

insert into #leave values(1,'2011-10-25','2011-10-27','Approved')
insert into #leave values(2,'2011-10-26','2011-10-27','Approved')
insert into #leave values(3,'2011-10-27','2011-10-27','Rejected')
insert into #leave values(1,'2011-11-01','2011-10-01','Approved')


select * from #leave

IF OBJECT_ID('#sample') IS NOT NULL
    DROP TABLE #sample 
 
SELECT * INTO #sample FROM #leave WHERE 1=2

DECLARE @LeaveID int, @EmpID int
DECLARE @StDate DATETIME, @EndDate DATETIME
DECLARE @status varchar(50)
DECLARE cur CURSOR FOR SELECT * FROM #leave
OPEN cur
FETCH FROM cur INTO @LeaveID,@EmpID,@StDate,@EndDate,@status
PRINT @EndDate
WHILE(@@FETCH_STATUS = 0)
BEGIN
    WHILE(@StDate <= @EndDate)
    BEGIN
        INSERT INTO #sample(EmployeeId,FromDate,[Status]) VALUES(@EmpID,@StDate,@status)
        SET @StDate = DATEADD(DD,1,@StDate)
    END
    FETCH FROM cur INTO @LeaveID,@EmpID,@StDate,@EndDate,@status
END
CLOSE cur

SELECT EmployeeId,FromDate as 'Date',[Status] FROM #sample


HOPE IT WORKS....
 
Share this answer
 
Here it is :

SQL
with a(i, j, k, l , m)
as
(
  select * from #leave
), b(i, j, k, l , m)
as
(
select a.i, a.j, a.k , a.l , m  from a
union all
select b.i, b.j, DATEADD(d, 1, b.k) , cast(b.l as datetime), b.m  from b
where b.k<b.l
)
select j EmployeeId, k Date, m Status from b
order by i, k




Thanks to the magic of CTEs ;)

Also excuse me for the inconvenient naming style.

Good Luck.
 
Share this answer
 
v2
select EmployeeId,DateDiff(d,FromDate,ToDate) as [Difference in Day],[Status] from #leave

This will shows the result as your need.
 
Share this answer
 
v2
hi
VB
select
FromDate,
ToDate,

DATEDIFF(dd,FromDate,ToDate) as DateDifferent

 from #leave



if this helps...do not hesitate to mark it..
 
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