Solved

SQL Query Alert 30 Day Reminder

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

728 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