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.
| Author |
Topic |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2004-09-12 : 03:42:46
|
| I have 2 fields fromyear and toyear (numbers)I want to be able to do a query that selects all records that the year is 2000 where 2000 is between fromyear and to year.What is the correct syntax? |
|
|
gpl
Posting Yak Master
195 Posts |
Posted - 2004-09-12 : 04:35:00
|
quote: Originally posted by esthera I have 2 fields fromyear and toyear (numbers)I want to be able to do a query that selects all records that the year is 2000 where 2000 is between fromyear and to year.What is the correct syntax?
I assume that you want to return all the rows for the fromyear up until the end of toyearwhere MyDate between convert(varchar(4),@fromyear) and convert(varchar(4),@toyear + 1) Graham |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2004-09-12 : 04:58:46
|
| I did the follwing SELECT * from alumni WHERE 2004 between fromdates and todatest+1 For some reason someone with the dates of 2000-2003 showed up. What am I doing wrong? The fromdates adn todates are integer fields. |
 |
|
|
gpl
Posting Yak Master
195 Posts |
Posted - 2004-09-12 : 06:24:46
|
| I see what you are doing, you are holding the years on the table and testing against a year - I think you might have NULL values in todate (ie they havent left yet!) - try setting the todate to the current year if null --WHERE 2004 between fromdates and Coalesce(todatest, Year(Getdate())Note that you dont need to add 1 to the end year (I suggested that when I thought you were using datetime values)Graham |
 |
|
|
|
|
|