We help IT Professionals succeed at work.
Get Started

SQL Script to join to dates

157 Views
Last Modified: 2016-04-07
Hello Experts Exchange
I have the following script;

DECLARE @startdate DATETIME
,@enddate DATETIME;
 
SET @startdate = '2016-03-06';
SET @enddate = '2016-03-20';
WITH calendardates
AS ( SELECT date1 = @startdate
UNION ALL
SELECT DATEADD(DAY, 1, date1)
FROM calendardates
WHERE DATEADD(DAY, 1, date1) <= @enddate
)
Select c.date1,[declared_at],[branch],[Day_Time2],[Shop_Time],[Shop_Time_Declared_At],[Store_Close_Time]
from [Bank_Activity_Declared_At_Time] BA
right JOIN calendardates c
ON DATEADD(dd, 0, DATEDIFF(dd, 0, BA.declared_at)) = c.date1
where branch = 227
and declared_at >='2016-03-06' 
and declared_at <= '2016-03-20'

Open in new window


There is two missing records for the 13th and the 18th, I want the date to return with all the other columns as null, I thought this query would do it but I must be missing something.

How do I fix the script?

Regards

SQLSearcher
Comment
Watch Question
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 2 Answers and 12 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE