If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > MS tSQL: Retrieving Anniversary Dates within 1 Week from Today?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-22-04, 15:00
Blitzkrieg Blitzkrieg is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
Red face MS tSQL: Retrieving Anniversary Dates within 1 Week from Today?

Doing math on dates in SQL always hurts my head ;P

What I would like to do is retrieve a recordset where the anniversary date
is within (less than or equal to) 7 days from today's date.

For example, if today is 03/22/2004, then an anniversary date of 03/18/2001
should be included in the recordset, because the 3rd anniversay is coming-up
within 7 days of 'today.'

Any clue? I'm using MS SQL Server 2000.
__________________
Sincerely,
Todd M. Taylor
http://www.toddtaylor.com/
Reply With Quote
  #2 (permalink)  
Old 03-22-04, 15:33
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Every database engine seems to use slightly different date arithmatic. For MS-SQL 7.0 and later I'd use:
PHP Code:
SELECT a.years
,  Convert(CHAR(10), DateAdd(yeara.yearsa.[myDate]), 121)
,  
Convert(CHAR(10), a.[myDate], 121)
   
FROM (SELECT [myDate], CASE WHEN DateAdd(year
,     DateDiff(year, [myDate], GetDate()), [myDate]) > GetDate() 
      
THEN DateDiff(year, [myDate], GetDate()) - 1
      
ELSE DateDiff(year, [myDate], GetDate())
   
END AS years
   FROM dbo
.tMyDates) AS a
   WHERE  DateDiff
(dayDateAdd(year
,     a.yearsa.[myDate]), GetDate()) < 7
   ORDER BY 2

You don't strictly have to keep the years and the event separate, but it makes things easier for me to manipulate.

-PatP
Reply With Quote
  #3 (permalink)  
Old 03-22-04, 15:55
Blitzkrieg Blitzkrieg is offline
Registered User
 
Join Date: Oct 2003
Posts: 14
Thanks for the solution, Pat.

I think I got one I like better though:
Code:
WHERE
DATEDIFF(dd,GETDATE(),DATEADD(yy,DATEDIFF(yy, Users.HireDate,GETDATE()),Users.HireDate)) 
BETWEEN 0 and 7
This *seems* to work with the data I have so far. I'm still trying to digest how it works to determine how it handles leap years.
__________________
Sincerely,
Todd M. Taylor
http://www.toddtaylor.com/
Reply With Quote
  #4 (permalink)  
Old 03-22-04, 16:04
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Try your example data from your first posting.

-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On