There are many questions similar to this one however I have been unable to locate one which can meet my requirements. So apologies if I have missed one....
I have a StartDate
and EndDate
like so:
Id StartDate EndDate
1 2015-02-05 09:00:00.0000000 +01:00 2015-02-09 17:30:00.0000000 +01:00
Now from this I am trying to list the dates and times (Including the StartDate
and EndDate
) between these 2 values like so:
Id StartDate EndDate
1 2015-02-05 09:00:00.0000000 +01:00 2015-02-05 17:30:00.0000000 +01:00 --does not include this date in the query below
1 2015-02-06 09:00:00.0000000 +01:01 2015-02-06 17:30:00.0000000 +01:00
1 2015-02-07 09:00:00.0000000 +01:01 2015-02-07 17:30:00.0000000 +01:00
1 2015-02-08 09:00:00.0000000 +01:02 2015-02-08 17:30:00.0000000 +01:00
1 2015-02-09 09:00:00.0000000 +01:02 2015-02-09 17:30:00.0000000 +01:00
There will be multiple Id
's in this table so grouping them by this would be extremely useful also.
Can this be done using this Table Structure?
CREATE TABLE TestDateRange (Id int identity (1,1) not null,
StartDate DATETIMEOFFSET,
EndDate DATETIMEOFFSET
UserID int)
ALTER TABLE TestDateRange ADD CONSTRAINT Id_PK PRIMARY KEY (Id)
Looking at the similar post mentioned, this code does not achieve my desired affect: (misses the startDate off which I am working on now)
SELECT
v.Id,
d.CalendarDate
FROM
Dates d INNER JOIN
TestDateRange v ON
d.CalendarDate >= v.StartDate AND
d.CalendarDate <= v.EndDate
group by
d.CalendarDate,
v.Id
SOLVED thanks to Bulat for the help:
d.CalendarDate >= CAST(v.StartDate AS DATE) AND