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
 Between keyword

Author  Topic 

rindharajan
Starting Member

6 Posts

Posted - 2009-10-13 : 10:08:42
HI i have 2 columns say Month having values (1 to 12)and year having values (2009 - 2015).

Now i want to retrieve values between Apr 2009 to Jun 2010...Please help me with the query.

Thanks,
RindhaRajan

winterh
Posting Yak Master

127 Posts

Posted - 2009-10-13 : 10:11:34
Where Date_Field Between Put_Start_Date_here AND Put_End_Date_Here

[/fail at query]
Go to Top of Page

rindharajan
Starting Member

6 Posts

Posted - 2009-10-13 : 10:13:12
But date is not a single field...Month and Year are in different field.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-10-13 : 10:14:06
OK

Reread the question

S/he has 2 columns

WHY? Do you have 2?

Maybe you can make a derived column s oyou can use all of the datetime functions



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-13 : 10:31:43
select * from table
where convert(varchar(4),yearColumn)+right('00'+convert(varchar(2),ltrim(rtrim(monthColumn))),2)
between '200904' and '201506'


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-13 : 10:55:12

where
dateadd(month,month_col-1,dateadd(year,year_col-1900,0))>=cast('Apr 2009' as datetime) and
dateadd(month,month_col-1,dateadd(year,year_col-1900,0))<dateadd(month,1,cast('Jun 2010' as datetime))


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

winterh
Posting Yak Master

127 Posts

Posted - 2009-10-13 : 11:19:06
I see, I never read the question properly. :) Cheers for the heads up my man, I will make sure to read questions properly in the future

[/fail at query]
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-10-13 : 11:47:51
problem is, with those solutions, is that they will have to scan



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-13 : 11:54:14
You are right!
It is a design problem.

But should we not give a solution because it is not a fine solution?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-10-13 : 14:56:26
Something like this might work (but it'll probalby scan too because of the OR):
SELECT *
FROM MyTable
WHERE
(Month > 3 AND Year = 2009)
OR (Month < 7 AND Year = 2010)
Go to Top of Page
   

- Advertisement -