473,889 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with MS SQL query

I'm using MS SQL 2000 server. I have a table which includes a date field
which has people's birthdays in it. How can I write a query to return all
the records of people with birthdays within the next 30 days? (Based on
system date as the starting point.) Actual year of birthday is irrelevant.
Also the next 30 days may span into the next month, or the next year.
Jul 20 '05 #1
7 16226

"Fred Thompson" <fr***@aol.co m> wrote in message
news:DISAb.4569 81$HS4.3574769@ attbi_s01...
I'm using MS SQL 2000 server. I have a table which includes a date field
which has people's birthdays in it. How can I write a query to return all
the records of people with birthdays within the next 30 days? (Based on
system date as the starting point.) Actual year of birthday is irrelevant. Also the next 30 days may span into the next month, or the next year.

Something like

select lname, birthdate from birthtable where birthdate> getdate() and
birthdate< getdate()+30


Jul 20 '05 #2
But this takes year into account, so your query will only select people born
30 days into the future.
I need the query to select anyone whose birthday is coming up in the next 30
days regardless of what year they were born.

"Greg D. Moore (Strider)" <mo*****@greenm s.com> wrote in message
news:OP******** *************@t wister.nyroc.rr .com...

"Fred Thompson" <fr***@aol.co m> wrote in message
news:DISAb.4569 81$HS4.3574769@ attbi_s01...
I'm using MS SQL 2000 server. I have a table which includes a date field which has people's birthdays in it. How can I write a query to return all the records of people with birthdays within the next 30 days? (Based on
system date as the starting point.) Actual year of birthday is

irrelevant.
Also the next 30 days may span into the next month, or the next year.


Something like

select lname, birthdate from birthtable where birthdate> getdate() and
birthdate< getdate()+30



Jul 20 '05 #3
"Fred Thompson" <fr***@aol.co m> wrote in message news:DISAb.4569 81$HS4.3574769@ attbi_s01...
I'm using MS SQL 2000 server. I have a table which includes a date field
which has people's birthdays in it. How can I write a query to return all
the records of people with birthdays within the next 30 days? (Based on
system date as the starting point.) Actual year of birthday is irrelevant.
Also the next 30 days may span into the next month, or the next year.


Here's a UDF that will find all birthdays N days from a given reference date.
For your case, N = 30 and the reference date = today.

CREATE TABLE Birthdays
(
person_name VARCHAR(25) NOT NULL PRIMARY KEY,
birthday DATETIME NOT NULL CHECK (birthday <= CURRENT_TIMESTA MP)
)

CREATE VIEW Today (d)
AS
SELECT CAST(CONVERT(CH AR(8), CURRENT_TIMESTA MP, 112) AS
DATETIME)

-- Returns all persons whose birthday is within @ndays of @ref_date
-- Assumes @ref_date has a time of 12AM
-- Default @ref_date is today
CREATE FUNCTION BirthdaysNDaysF romDate
(@ndays INT, @ref_date DATETIME = NULL)
RETURNS TABLE
AS
RETURN(
SELECT person_name,
birthday,
COALESCE(@ref_d ate, (SELECT d FROM Today)) AS reference_date
FROM Birthdays
WHERE birthday <=
COALESCE(@ref_d ate, (SELECT d FROM Today)) + @ndays AND
(YEAR(COALESCE( @ref_date, (SELECT d FROM Today))) <
YEAR(COALESCE(@ ref_date, (SELECT d FROM Today)) + @ndays) OR
(DATEADD(YEAR,
YEAR(COALESCE(@ ref_date, (SELECT d FROM Today))) -
YEAR(birthday),
birthday) BETWEEN
COALESCE(@ref_d ate, (SELECT d FROM Today)) AND
COALESCE(@ref_d ate, (SELECT d FROM Today)) + @ndays)) AND
(YEAR(COALESCE( @ref_date, (SELECT d FROM Today))) =
YEAR(COALESCE(@ ref_date, (SELECT d FROM Today)) + @ndays) OR
DATEADD(YEAR,
YEAR(COALESCE(@ ref_date,
(SELECT d FROM Today))) - YEAR(birthday),
birthday) >=
COALESCE(@ref_d ate, (SELECT d FROM Today)) OR
DATEADD(YEAR,
YEAR(COALESCE(@ ref_date, (SELECT d FROM Today)) +
@ndays) - YEAR(birthday),
birthday) <=
COALESCE(@ref_d ate, (SELECT d FROM Today)) + @ndays)
)

-- Uses today as the reference date
CREATE FUNCTION BirthdaysNDaysF romToday (@ndays INT)
RETURNS TABLE
AS
RETURN(
SELECT *
FROM BirthdaysNDaysF romDate(@ndays, DEFAULT)
)

INSERT INTO Birthdays (person_name, birthday)
VALUES ('Joe', '19801231')
INSERT INTO Birthdays (person_name, birthday)
VALUES ('Jim', '20000101')
INSERT INTO Birthdays (person_name, birthday)
VALUES ('Jerry', '19500610')

-- All birthdays 30 days from today, 20031208
SELECT *
FROM BirthdaysNDaysF romToday(30)

person_name birthday reference_date
Jim 2000-01-01 00:00:00.000 2003-12-08 00:00:00.000
Joe 1980-12-31 00:00:00.000 2003-12-08 00:00:00.000

-- All birthdays 180 days from 19811215
-- Given this reference date, Jim wasn't born yet
SELECT *
FROM BirthdaysNDaysF romDate(180, '19811215')

person_name birthday reference_date
Jerry 1950-06-10 00:00:00.000 1981-12-15 00:00:00.000
Joe 1980-12-31 00:00:00.000 1981-12-15 00:00:00.000

Regards,
jag
Jul 20 '05 #4
"Fred Thompson" <fr***@aol.co m> wrote in message
news:DISAb.4569 81$HS4.3574769@ attbi_s01...
I'm using MS SQL 2000 server. I have a table which includes a date field
which has people's birthdays in it. How can I write a query to return all
the records of people with birthdays within the next 30 days? (Based on
system date as the starting point.) Actual year of birthday is irrelevant. Also the next 30 days may span into the next month, or the next year.

Here is a half-baked idea (totally untested as well):

select lname, birthdate
from birthdaytable
where datediff(year, birthdate, getdate()) < datediff(year, birthdate,
dateadd(day,30, getdate()))

Assuming datediff(year, ...) rounds down.
I think that this will be a poor performer in that it should force a table
scan but depending on how large the birthday table is, this may be
irrelevant.

Hope this helps
Ronnie
Jul 20 '05 #5
"Fred Thompson" <fr***@aol.co m> wrote in message news:<DISAb.456 981$HS4.3574769 @attbi_s01>...
I'm using MS SQL 2000 server. I have a table which includes a date field
which has people's birthdays in it. How can I write a query to return all
the records of people with birthdays within the next 30 days? (Based on
system date as the starting point.) Actual year of birthday is irrelevant.
Also the next 30 days may span into the next month, or the next year.

Hi Fred,

The simplest way I can think of is create a temp table with the next
30 days and join it against your birthday table on month()=month() and
day()=day(). The code would be like:

create table #T(bdays datetime)
declare @i int, @d datetime
select @d=getdate()
select @d=cast(year(@d ) as varchar)
+'/'+cast(month(@d ) as varchar)
+'/'+cast(day(@d) as varchar)

set @i=0
while @i<=30 begin
insert into #T
select bdays=dateadd(d ay,@i,@d)
set @i=@i+1
end

select a.bdays
from birthdays as a
join #T as b
on month(a.bdays)= month(b.bdays)
and day(a.bdays)=da y(b.bdays)
Jul 20 '05 #6
I didn't test it, but you'll get the idea:

SELECT Name, birthday, DATEDIFF(yy, birthday, getdate()) as years_now,
DATEDIFF(yy, birthday, (getdate() +30) ) as years_will_be
FROM table
WHERE DATEDIFF(yy, birthday, getdate()) < DATEDIFF(yy, birthday, (getdate()
+30) )
-----
Hope this helps
"Fred Thompson" <fr***@aol.co m> wrote in message
news:DISAb.4569 81$HS4.3574769@ attbi_s01...
I'm using MS SQL 2000 server. I have a table which includes a date field
which has people's birthdays in it. How can I write a query to return all
the records of people with birthdays within the next 30 days? (Based on
system date as the starting point.) Actual year of birthday is irrelevant. Also the next 30 days may span into the next month, or the next year.

Jul 20 '05 #7
select *
from table
where datediff(day, getdate(), dateadd(year, (year(getdate() ) -
year(birthday)) , birthday)) < 30

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3061
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
9
3144
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
6
2430
by: paii | last post by:
I have a table that stores job milestone dates. The 2 milestones I am interested in are "Ship Date" TypeID 1 and "Revised Ship Date" TypeID 18. All jobs have TypeID 1 only some jobs have TypeID 18. I need a query that will return the c date for TypeID 18 if it exist else the date for TypeID 1, for all jobs. the table structure is the following Job TypeID
3
1869
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype, answer) They are related by quesnum and questype. There are records in
7
2377
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller component parts. The subpart would then be listed in the Part field linked to each of its subparts in...
3
10687
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays the record's ID number. When I add the source table to the query it makes several records...
0
2267
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script in hopes on not having to trouble others on those. But here it goes...
10
2603
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only tool you know how to use is a hammer, every problem tends to look like a nail. That said, I could solve my problem in C, but it's not the right tool. I need to come into the Windows world, and I need to get this done in Access or something...
7
2043
by: Rnykster | last post by:
I know a little about Access and have made several single table databases. Been struggling for about a month to do a multiple table database with no success. Help! There are two tables. First has about 30 fields. Every entry in this table will be unique. Second table has about 7 fields and is for reference - strictly a look up type table. I want to use one field, say FAMILY in the first table to look up any one of the 400 items in the...
3
2566
by: pbd22 | last post by:
Hi. I need some help with structuring my query strings. I have a form with a search bar and some links. Each link is a search type (such as "community"). The HREF for the link's anchor looks like the following: <a href="?searchtype=2">Community</a>
0
9967
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9810
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4650
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.