Introduction:
In this article I am going to explain How to get the birthday of employees
falling within week from current date.
In
previous articles I explained How to Get day,month and year from current or any date
and Multiple queries to get all dates between two dates and Query to get first, last date and total number of days in month and All Date formats in SQL and Insert into select from statement to copy data from one table to another
Description:
While working on project I got the requirement to show all the employees whose
birthday is within a week. So I created the query mentioned below and sharing
with you all so that it can help others in similar situation.
Implementation:
Let’s create a temporary table with dummy data for demonstration purpose
CREATE TABLE #TEMP
(
EmployeeId INT IDENTITY(1,1) PRIMARY KEY,
EmployeeName VARCHAR(100),
DateOfBirth DATE
)
INSERT INTO #TEMP (EmployeeName, DateOfBirth) VALUES
('Aman','1985-05-16'),
('kiran','1980-01-28'),
('Anuj','1990-05-12'),
('Pawan','1987-05-19'),
('Abhishek','1983-05-09'),
('Sunil','1989-10-29'),
('Salman','1978-05-11'),
('Arman','1988-09-30'),
('Sunny','1991-11-06'),
('Deepika','1977-05-15'),
('Anu','1993-05-15'),
('Heena','1995-05-18'),
('Nancy','1990-05-17'),
('Anu','1993-05-02'),
('Ranbir','1990-08-14');
--Query to get birthdays
within week
SELECT EmployeeName,DateOfBirth FROM
#TEMP
WHERE DATEADD (YEAR, DATEPART(YEAR, GETDATE()) - DATEPART(YEAR, DateOfBirth), DateOfBirth)
BETWEEN CAST(GETDATE() AS DATE) AND CAST(DATEADD(WEEK, 1, GETDATE())-1 AS DATE)
ORDER BY DAY(DateOfBirth),
EmployeeName
Result:
EmployeeName
|
DateOfBirth
|
Anuj
|
1990-05-12
|
Anu
|
1993-05-15
|
Deepika
|
1977-05-15
|
Aman
|
1985-05-16
|
Nancy
|
1990-05-17
|
Heena
|
1995-05-18
|
Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates.
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..