Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Query Alert 30 Day Reminder

Posted on 2014-10-10
5
Medium Priority
?
340 Views
Last Modified: 2014-11-24
I have a SQL Database that is back end to a program we use for accounting and business mgt.  Inside the program, we can write multiple alerts based on query's of the data stored in SQL.  The current query i have written displays an alert when someone has a birthday.

set transaction isolation level read uncommitted
SELECT
AlertMessage = 'Happy Birthday '+firstname+' '+lastname+' !!'
FROM dbo.Employees
WHERE DATEPART(d, birthdate) <= DATEPART(d, GETDATE())
AND DATEPART(m, birthdate) = DATEPART(m, GETDATE())

I would like to modify it so that it shows the alert 30 days before the birthday is going to happen.
0
Comment
Question by:thirdrockit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40373960
SELECT 
AlertMessage = ' 30 days before you wish Happy Birthday '+firstname+' '+lastname+' !!'
FROM dbo.Employees
WHERE DATEPART(d, birthdate) <= DATEPART(d, GETDATE() - 30)
AND DATEPART(m, birthdate) = DATEPART(m, GETDATE() )

Open in new window

0
 

Author Comment

by:thirdrockit
ID: 40373984
after running it, it shows 2 people who had a birthday yesterday 10/9.  If there birthday has already passed, should the alert still run?
0
 

Author Comment

by:thirdrockit
ID: 40373999
In other words, it needs to alert for 30 days and then on the day, don't alert anymore.
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40374001
As usual with birthdays, leap birth days are a pita.  Not sure how this code handles those.

For exactly 30 days ago only:

WHERE
    birthdate = DATEADD(DAY, +30, DATEADD(YEAR, -(YEAR(GETDATE()) - YEAR(birthdate)), CAST(GETDATE() AS date))) AND
    GETDATE() < DATEADD(DAY, 1, DATEADD(YEAR, YEAR(@current_date) - YEAR(birthdate), CAST(birthdate AS date)))
0
 

Author Comment

by:thirdrockit
ID: 40377877
I declared @current_date as integer.  The query ran with no errors but it doesn't give me any results.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question