Solved

SQL Query Alert 30 Day Reminder

Posted on 2014-10-10
5
308 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 69

Accepted Solution

by:
Scott Pletcher earned 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

624 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