Solved

SQL Query Alert 30 Day Reminder

Posted on 2014-10-10
5
237 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
  • 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:
ScottPletcher 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now