SQL Query

Experts - May be this is a stupid question but my mind isnt working to crack this one up.

I have a leave table

LeaveID         SubmitterName        LeaveType        FromDate       ToDate
1                      ABC                              Full Day             2/7/2018         2/8/2018
2                      DEF                              Full Day             2/12/2018        2/12/2018
3                      XYZ                               Full Day            2/12/2018        2/16/2018

In the above table, ABC is taking leave for 2 days from 7th to 8th (both days inclusive), DEF is taking leave only for 1 day (2/12/2018) and XYZ is taking leave for 5 days from 2/12 to 2/16.

What I need is
Considering the above example,
1. First I will get the min & max date from above table to know the list of dates. Ex: 2/7/2018 to 2/16/2018. (which would be 10 rows).
2. Then I need to match from date & to date to that list of 10 days. Ex: only 7 days (2/7, 2/8, 2/12, 2/13, 2/14, 2/15, 2/16)
3. Then i need to fill the other data.

Example output

LeaveID   SubmitterName         LeaveType           LeaveDate
1                ABC                             Full Day                   2/7/2018
1                ABC                             Full Day                   2/8/2018
2                DEF                              Full Day                  2/12/2018
3               XYZ                               Full Day                   2/12/2018
3               XYZ                               Full Day                   2/13/2018
3               XYZ                               Full Day                   2/14/2018
3               XYZ                               Full Day                   2/15/2018
3               XYZ                               Full Day                   2/16/2018

Can someone help me please?
LVL 7
ManjuIT - Project ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try this:

declare @leave table
(
	LeaveID int identity,
	SubmitterName varchar(100),
	LeaveType varchar(20),
	FromDate datetime,
	ToDate datetime
)
insert into @leave
values
('ABC', 'Full Day', '2/7/2018', '2/8/2018'),
('DEF', 'Full Day', '2/12/2018', '2/12/2018'),
('XYZ', 'Full Day', '2/12/2018', '2/16/2018');

select a.LeaveID, a.SubmitterName, a.LeaveType, dateadd(d, v.number, a.FromDate)
from @leave a
join master..spt_values v on v.type='P' and v.number between 0 and datediff(d, a.FromDate, a.ToDate)
order by 1, 4

Open in new window


ref:

SQL how to convert row with date range to many rows with each date
https://stackoverflow.com/questions/16358959/sql-how-to-convert-row-with-date-range-to-many-rows-with-each-date
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.