OiO.lk Blog SQL Left join to a Date range + SQL Math with Dates
SQL

Left join to a Date range + SQL Math with Dates


second week working in SQL and data. I am commenting/documenting views we have in our database and I am so lost on this specific view. I have never seen a join into a date range and am quite puzzled by it. As well as there’s some math involving dates that I can’t wrap my head around for some reason but I probably just need some more coffee – although some help/advice with it would be much appreciated. Anyways here it is:

Left Join

     Date1

ON

     Date1 > Date2

     AND Date1 < Date3

With this I’m confused what exactly is being left joined. Is Date1 being joined by matching data from the Date2 and Date3 table IF their respective info falls within that date range? Or are Date2 and Date3 being matched by data in Date1 IF it’s data falls within this range. There is no key = key match that I am accustomed to with joins.

date1 < GETDATE() + 7 - DATEPART(dw, GETDATE()) + 1

With this what trips me up is going from a DD/MM/YYYY and DD format. Date1 is in DD/MM/YYYY, you add 7 (days?) to today (10/16/2024), then subtract by the days value (today is wednesday so it would be 4) so we get 10/19/2024? Or is it just 19? Then you add one so its 10/20/2024 or just 20? Then is it okay to compare dates if they are in different formats? I am definitely missing the knowledge in this so if anyone could enlighten me on this that would be awesome.



You need to sign in to view this answers

Exit mobile version