SQL Query to find All Sundays Between Two Dates
Last Updated :
07 Apr, 2021
To find all the Sundays in between two days using SQL Language, we will be using the "Date Functions" defined in SQL. Apart from these we will be using CTE ( View) idea too.
Basic idea:
So basically we are given two days, and we are required to list all Sundays between these two days.
Thinking a little we got the idea that we can check all the dates between the two boundary dates given for Sundays. Also SQL Date functions do provide one such method to implement this idea:
DATENAME(interval_part, Date)
This function return the name of the part of the date specified in arguments.
For eg :
DATENAME(month, '18/11/2001' )
This call to the function would return November.
Hence we can match the name of day represented by any date with sunday. If it is sunday than we select it otherwise rejects it.
Now the left over part is to create a table which contains a column of all the dates between the two given dates, so that we can perform our check over them.
This can be done using the idea that we can increment the day part of date by 1 over the preceding date to get next date and do check that the next date is less than the given upper bound of the dates.
DATEADD(part , number, date) : This method is used to add the specified number to the given part of the date .
For eg. DATEADD(year,2,'18/11/2001') This results in the date : 18/11/2003.
So here is our SQL Query for this topic:
declare @StartDate DateTime = '2021-02-01',
@EndDate DateTime = '2021-02-28';
/*Creating a temporary view in sql(CTE) which recursively
calls itself to find next date by incrementing the
previous date and stores the result in it till the end date is reached*/
WITH CTE(date_list) AS (
SELECT @StartDate
UNION ALL
SELECT DATEADD(day,1,date_list) FROM CTE
WHERE date_list<=@EndDate
)
SELECT date_list as 'List of sundays'
FROM CTE
WHERE DATENAME(weekday ,date_list) IN ('Sunday');
/*In the where clause at last we are checking
each day from the list whether it is in Sunday list or not*/
Similar Reads
How to Get all Dates Between Two Dates in SQL Navigating and manipulating date ranges is a common task in SQL, especially when we need to generate lists of dates between two specified dates. This article covers two methods using the SQL GROUP BY clause: the traditional method with explicit column names and the shorter "GROUP BY 1" syntax. Weâll
4 min read
SQL Query to Compare Two Dates In SQL, dates are complicated for newbies, since while working with the database, the format of the date in the table must be matched with the input date in order to insert. In various scenarios instead of date, DateTime (time is also involved with date) is used. Here we will see, SQL Query to compa
2 min read
How to Select Data Between Two Dates and Times in SQL Server? In SQL, some transactions need to be extracted based on their completion times and dates. Here, the DATETIME2 data type in SQL to perform such operations. For this article, we will be using the Microsoft SQL Server as our database. Note: Here, we will use the two dates and times given in the query a
2 min read
SQL Query to Calculate Total Number of Weeks Between Two Dates Calculating the total number of weeks between two dates is a common requirement in data analysis and reporting. This calculation is important for determining time intervals in weeks, which can be useful in various scenarios, such as tracking project timelines, analysing sales trends, or measuring cu
3 min read
SQL Query to Check if Date is Greater Than Today in SQL Managing dates effectively is critical in SQL for performing comparisons and scheduling. One common scenario is to check if a date in a database is greater than todayâs date. In SQL, this can be achieved using the GETDATE() function, which retrieves the current system date and time in the format YYY
4 min read
Count the number of a special day between two dates by using PL/SQL Prerequisite - PL/SQL Introduction, Decision Making in PL/SQL Write a pl/sql program to input two dates and print number of Sundays between those two dates. Explanation: Before each iteration of the loop, condition is evaluated. If it evaluates to TRUE, sequence_of_statements is executed. If conditi
2 min read