SQL Query to Get First and Last Day of a Week in a Database
Last Updated :
09 May, 2021
An SQL query is a single line statement of a Program written in a particular language to perform a specific task. A query consists of some pre-defined functions in SQL like SELECT, CREATE, etc. So, in this article, we will learn about SQL query to get the first and last day of a week in a database. So let's create a database first to do our task, here we will be using Microsoft SQL Server:
Step 1: Create a database named as GFG:
CREATE DATABASE GFG
Step 2: Use this database:
USE GFG
Step 3: Create a table in this database:
CREATE TABLE geeksforgeeks(
id integer,
FirstDay varchar(20)
)
Describe this table:
sp_help 'dbo.geeksforgeeks'
Step 4: Insert the values of dates in this database:
INSERT INTO [dbo].[geeksforgeeks]
([id] ,[FirstDay])
VALUES
(1, 'Monday')
GO
INSERT INTO [dbo].[geeksforgeeks]
([id] ,[FirstDay])
VALUES
(2, 'Tuesday')
GO
INSERT INTO [dbo].[geeksforgeeks]
([id] ,[FirstDay])
VALUES
(3, 'Wednesday')
GO
INSERT INTO [dbo].[geeksforgeeks]
([id] ,[FirstDay])
VALUES
(4, 'Thursday')
GO
INSERT INTO [dbo].[geeksforgeeks]
([id] ,[FirstDay])
VALUES
(5, 'Friday')
GO
INSERT INTO [dbo].[geeksforgeeks]
([id] ,[FirstDay])
VALUES
(6, 'Saturday')
GO
INSERT INTO [dbo].[geeksforgeeks]
([id] ,[FirstDay])
VALUES
(7, 'Sunday')
GO
Now, we are checking that the data is inserted successfully or not:
SELECT * FROM [dbo].[geeksforgeeks]
Step 5: Now check the first day of the week:
SELECT id, FirstDay FROM [dbo].[geeksforgeeks] WHERE id = @@DATEFIRST
So we can see the first day of the week is Sunday
Step 6: Now we will find out what is the first day of the next week using the DATEADD function():
/*
If you want to find out the first day of
upcoming weeks then set @weeks as a positive integer
with number of weeks, else negative integer.
*/
DECLARE @weeks integer;
SET @weeks = 1;
SELECT DATEADD(WEEK, @weeks, DATEADD(DAY, 1-DATEPART(WEEKDAY, GETDATE()),
DATEDIFF(dd, 0, GETDATE()))) as 'First Day of next week';
Step 7: Now we will find out what is the last day of the next week using the DATEADD function:
/*
If you want to find out the first day of
previous weeks then set @weeks as a negative integer
with number of weeks, else negative integer.
*/
DECLARE @weeks integer;
SET @weeks = 1;
SELECT DATEADD(wk, +2, DATEADD(DAY, 0-DATEPART(WEEKDAY, GETDATE()),
DATEDIFF(dd, 0, GETDATE()))) as 'Last Day of Next Week';
Similar Reads
SQL Query to Get First and Last Day of a Month in a Database In SQL, working with date and time data is critical for tasks like reporting, scheduling, and data analysis. Determining the first and last day of a month is a common requirement. SQL provides powerful functions like DATE_SUB, DAYOFMONTH, and LAST_DAY to make these calculations straightforward.In th
4 min read
SQL Query to Set First Day of the Week in a Database In this article, we are going to write a SQL script to Set the first day of the week in a Database. Below is the implementation for the same. You should have MSSQL in your server. Query in SQL - A query is a request for data or information from a database table or combination of tables. This data ca
2 min read
How to Write a SQL Query For a Specific Date Range and Date Time? Managing date and time in SQL is a critical task for many real-world applications, especially when handling time-sensitive data such as logs, timestamps, and event records. SQL provides various tools and data types to work with date and time effectively. In this guide, we will focus on working with
4 min read
How to Create Daily, Weekly, and Monthly Report in SQL Server? SQL Server is a versatile database and a most used database throughout the world. In this article, let us see SQL queries how to get Daily, Weekly, and Monthly reports from SQL Server. Let us start in creating a database and sample details Step 1: Database creation Command to create the database. He
3 min read
How to Get the First Day of the Month in PL/SQL? In PL/SQL programming, efficiently manipulating dates is crucial for various tasks, such as generating reports, calculating durations, or scheduling events. One common requirement is retrieving the first day of the month from a given date. In this article, we'll explore different approaches to achie
4 min read
Convert Date to Day of Week in R In this article, we are going to see different approaches to convert date to day of the week in R Programming language. The three different types of approaches to convert given dates to the day of the week in R language are as follows: Converting date to day of the week using weekdays functionConver
3 min read