August 10, 2005 at 5:27 am
Hi,
My DB is a MS SQL 2000 and this specific DB stores projects. Projects that can occur once during a day, but they can also last 3-4 days or up to 3-4 years.
My question is how a query would look like where I want to find out all projects that are active right now, today, or if I want to see all projects that are planned, for eample, a year from now?
To start with, do I need more than the two columns I have which contains the "Start-date" and the "End-date" of the projects?
Besides the information I "of course" need to display who ownes the project, which organisation he/she belongs to id# etc. etc.
How can a write a query that will work on several projects that last from 1, 2 days up to several years? For example I'm looking for planned projects "01 15 2006" and I have the following projects stored:
If I write a query as follows it will only apply on dates which differs with 3 days and the result would be Project 1 and 4.
select * from projects
where getdate(01 15 2006) between startdate and enddate
I need a query that applies on all four projects... Anyone who can help me?
/Snugge
August 10, 2005 at 5:56 am
I do not see why your query should not work, as long as:
a) StartDate and EndDate are both DateTimes
b) In the WHERE clause, you are careful with the getdate() bit. Remember that Getdate() returns a datetime, not just a date, and that might mean that you don't get the projects that have a start or end date on the same date. There are techniques for handling this.
Regards
August 10, 2005 at 6:03 am
I think you can assume that if getdate() is between a projects start and end date, then it is active; that's pretty straight forward.
select * from projects where getdate() between startdate and enddate
projects planned or active for a year from today:
select * from projects where dateadd(year,1,getdate()) between startdate and enddate
projects that are active at any time in the calendar year 2005:
select * from projects where startdate between '01/01/2005' and 12/31/2005 OR enddate between '01/01/2005' and 12/31/2005
HTH.
Lowell
August 10, 2005 at 6:31 am
Hi and thanks!
As you can see I'm new at this and the obvious question to follow would be where I can find out more about the specific "problem", what to do and not when it comes to "getdate()"? You see, I really can't make it work with the query I presented earlier, hmmm...
Thanks in advance.
August 10, 2005 at 6:37 am
It's pretty straightforward. Getdate() takes no parameters and always returns the current (system) datetime. If you want other dates based on 'now', you'll have to use additional functions (eg for adding days, months, years etc to getdate).
Lowell's post shows you how to select projects that are planned to be active a year from 'now'. What else are you trying to do?
August 11, 2005 at 5:15 am
Well, I do understand what you have presented for me, but I now have to accept that it really is not as simple as I thought. I'm afraid I have to be more specific and really annoy you guys. I believe that these queries you have presented are a bit simple or have to be combined with each other in a larger query.
Lets assume a project have started a year ago (2004) and will last three years from that date (2007). Project 2 started today, Aug 2005, and lasts 1 year and the third project will start January 2006 and which lasts 6 months.
Name Startdate Enddate
Somehow I believe that a query would go through the following steps:
Hopefully you know understand what kind of Query I'm looking for. I'm really grateful for your help and time you are putting in to my question, Thanks!
August 11, 2005 at 5:58 am
I must be missing something, but here's how I would answer questions (1) and (2)
1) select * from projects where '2006-03-01 00:00' between StartDate and EndDate
2) select * from projects where '2005-05-01 00:00' between StartDate and EndDate
August 11, 2005 at 6:11 am
Phil's reply is spot on; exactly what I would have suggested for a specific date it identifies all projects open on the date passed in;
and if you wanted to test if it was open during march 2006 at all, you'd want something like this:anything that was OPEN in may, regardless of period, would be this:
select * from projects where startdate < = '03/31/2006' AND enddate >= '03/01/2006'
modifing the example I provided earlier:
select * from projects where startdate between '03/01/2006' and '03/31/2006' OR enddate between '03/01/2006' and '03/31/2006'
that would find anything that started in May or anything that ended in may;
Lowell
August 11, 2005 at 2:04 pm
For #2, another way is:
select * from projects
where (DATEPART(year, startdate) = 2005 and DATEPART(month, startdate) = 5 )
or (DATEPART(year, enddate) = 2005 and DATEPART(month, enddate) = 5)
August 11, 2005 at 2:08 pm
This is also a great way to garantee an index scan... this is not a sarg condition.
August 11, 2005 at 2:17 pm
This might be better, otherwise there's an implicit 00:00:00 for the time portion:
select * from projects where startdate between '03/01/2006' and '03/31/2006 23:59:59' OR enddate between '03/01/2006' and '03/31/2006 23:59:59'
August 12, 2005 at 1:58 am
Thank you all!
I tried some of the examples and it really works. What really helped me off was the possibility to use '<=>' together with dates. Before today I thought that '<=>' only where to be used with numbers. Besides that I now have several ways to get exactly what I want.
Thanks a lot, I'm deeply grateful!
/Snugge
Viewing 12 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply