Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Generating automated emails from birthdays

Author  Topic 

MrNobody
Starting Member

5 Posts

Posted - 2007-03-14 : 14:51:01
Hello,
I just joined the forum and consider myself a SQL beginner. I have installed and supported, run backups and similar, but haven't done much with queries and scripting.

I have a MS SQL 2000 server with a company database on it. Amongst other things, the database contains names, birthdays and e-mail addresses. How difficult would it be to setup an automated query to run every morning comparing birthdays to the current date and send automated e-mails out to matching people saying "Happy birthday ...."?

Thanks in advance,
MrNobody

"If I'm not there, I must be somewhere else"

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2007-03-15 : 06:41:01
1. automated query....investigate SQL Agent
2. comparing birthdays to the current date...small SQL query involving = and/or the dateadd function.
3. email....search here for 'solutions' already posted on this topic....think there is one posted under the FAQ/Article sections.

4. BOL will be a good friend to you as you progress....as may we (IF you are seen to be helping yourself)
Go to Top of Page

MrNobody
Starting Member

5 Posts

Posted - 2007-03-15 : 14:44:06
Thank you for your response.
It gave me the most important answer which is yes, it is possible to do what I'm looking to do. After delving deeper, I've found that the query and sql agent parts won't be too difficult. I've almost got the query doing exacly what I want. I just need to figure out the best way to remove duplication of clients who have done business with us multiple times. I didn't have much success with DISTINCT so I'm looking at grouping.
I'll finish the query up and then start digging deeper into the email side of things.

MrNobody :)

"If I'm not there, I must be somewhere else"
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2007-03-16 : 06:48:17
With distinct only look at the fields which guarantee uniqueness, and then if you need to join back to get some non-unique info, you may have to choose the min/max of the non-unique info...

murphy, andrew, emailaddress1
murphy, andrew, emailaddress2

select distinct surname, firstname from mytable
or
slect surname, firstname, min(email) from mytable group by surname, firstname
Go to Top of Page

MrNobody
Starting Member

5 Posts

Posted - 2007-03-16 : 07:28:08
It seems the problem I was running into with Distinct is that all the data isn't in one table, so I am doing some joins. I would like to use the Social Sec number as the field I want to filter by. The Soc isn't stored in the same table as the name or email address, so I've joined them, linked by a common field for the transaction ID. Distinct works fine when I am just doing one table but stops working when I'm joining tables. When I get to the office, I wil try min.
Thanks

"If I'm not there, I must be somewhere else"
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2007-03-16 : 08:52:43
distinct shouldn't have an issue with joins.
post some smaple DDL and sample input data and we'll try to work something out.
Go to Top of Page

MrNobody
Starting Member

5 Posts

Posted - 2007-03-16 : 09:32:53
I'm not 100% sure I'm going to give this in the right format, but ....
Here is the basic code I'm trying to use:

SELECT DISTINCT Extra.f108 AS BorSoc, Files.iFileID,
Files.f19 AS LO, Files.f100 AS BorFirst, Files.f101 AS BorLast, Files.f118 AS BorBD,
Extra.f112 AS BorEmail
FROM Extra
INNER JOIN Files ON Extra.iFileID = Files.iFileID
WHERE (LEFT(Files.f118, 5) = LEFT(CONVERT(varchar, GETDATE(), 101), 5))

Extra:
iFileID|f108 |f112
10001 |123-45-1000|joe@test.com
10002 |123-45-2000|bob@test.com
10003 |123-45-2000|bob@test.com
10004 |123-45-3000|Tom@test.com
10005 |123-45-4000|sam@test.com
10006 |123-45-1000|joe@test.com

Files:
iFileID|F19 |f100|f101 |f118
10001 |John|Joe |Black|03/16/1969
10002 |Mike|Bob |Smith|03/15/1976
10003 |Mike|Bob |Smith|03/15/1976
10004 |Dan |Tom |Jones|03/16/1976
10005 |Ron |Sam |Raime|03/15/1976
10006 |John|Joe |Black|03/16/1969

(f19 is the rep in the company who worked with them)

In this example, I wouldn't want to see Bob Smith and Joe Black on the results twice. With my data, the distinct seems to make no difference.

Thanks

"If I'm not there, I must be somewhere else"
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2007-03-16 : 12:26:44
why aren't you just doing....??...since the requirement is to just get a list of email addresses?

SELECT DISTINCT Extra.f112 AS BorEmail
FROM Extra
INNER JOIN Files ON Extra.iFileID = Files.iFileID
WHERE (LEFT(Files.f118, 5) = LEFT(CONVERT(varchar, GETDATE(), 101), 5))


also....be aware of date conversion issues, especially from text values...ie 12/1 the 1st dec or 12th jan? best format is ccyymmdd.
Go to Top of Page

MrNobody
Starting Member

5 Posts

Posted - 2007-03-16 : 12:48:39
The 1st goal is to get a list of emails and send them a personalized e-mail saying "Happy Birthday"
2nd part will be to send an e-mail to the Sales Person who deals with that customer saying "It is [client's name]'s birthday today." so that we can call them in person. This email will need the name and contact info as well as other information I'll worry about later.

Lastly it has been requested that the manager also get a list of all birthdays each day including the Borrower name, the Contact working with them and the contact info so the manager can have people call on deals where the primary contact at the company no longer works here ...

Fun fun!

"If I'm not there, I must be somewhere else"
Go to Top of Page
   

- Advertisement -