Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
leave request table created in database

example data inserted like this
EMPNAME  leaveType     fromdate	    to_date   

Nisanam leaveApproved  12/08/2020	14/08/2020 


What I have tried:

employee taken 3 days leave so we need data like this. how we get


EMPNAME  leaveType   leave_date 

Nisanam leaveApproved 12/08/2020
Nisanam leaveApproved 13/08/2020
Nisanam leaveApproved 14/08/2020
Posted
Updated 18-Nov-20 22:45pm
v2
Comments
OriginalGriff 19-Nov-20 2:21am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
chilukuri sumanth reddy 19-Nov-20 2:45am    
now updated question

1 solution

Start with a tally table[^] containing enough numbers to cover the maximum length of leave. Combine that with DateAdd to expand the leave days, and filter out any days beyond the end date of the request.
SQL
WITH E1(N) AS -- 5 rows
(
    SELECT 1 
    UNION ALL SELECT 1 
    UNION ALL SELECT 1 
    UNION ALL SELECT 1 
    UNION ALL SELECT 1 
),
E2(N) AS -- 25 rows
(
    SELECT 1 
    FROM E1 a CROSS JOIN E1 b
),
E4(N) AS -- 625 rows
(
    SELECT 1 
    FROM E2 a CROSS JOIN E2 b
),
E(N) As
(
    SELECT ROW_NUMBER() OVER (ORDER BY N) - 1
    FROM E4
)
SELECT
    LeaveRequests.EMPNAME,
    LeaveRequests.LeaveType,
    DateAdd(day, E.N, LeaveRequests.FromDate) As LeaveDate
FROM
    LeaveRequests
    CROSS JOIN E
WHERE
    DateAdd(day, E.N, LeaveRequests.FromDate) <= LeaveRequests.ToDate
;
 
Share this answer
 
Comments
chilukuri sumanth reddy 20-Nov-20 2:44am    
send me dateadd function
Richard Deeming 20-Nov-20 3:19am    
Richard Deeming 20-Nov-20 3:22am    
chilukuri sumanth reddy 20-Nov-20 3:57am    
DATEADD function not there in my database how that query will work
Richard Deeming 20-Nov-20 4:20am    
DATEADD is a built-in function in all versions of SQL Server.

If you're not using SQL Server, then you need to update your question to provide proper details of which DBMS you are using.

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