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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Help to choose the data between dates

Author  Topic 

kkm
Starting Member

15 Posts

Posted - 2008-02-04 : 11:29:20
I have a database table having two coulms month and Year containing data for 2007 and 2008 Years. Month valuse are from 1, 2, 3 upto 12 and year values 2007 & 2008
I want fetch rows from march 2007 to February 2008 . I have gone little out of touch on querys I tried to use between operator but it did not work
I need help in where clause to choose the data from March 2007 to February 2008
select * from tablename where
from march 2007 to February 2008

Thank you in advance
KKM

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-04 : 11:43:47
select * from tablename
where (month >=3 and year=2007)
or (month<=2 and year=2008)
Go to Top of Page

kkm
Starting Member

15 Posts

Posted - 2008-02-04 : 12:04:52
quote:
Originally posted by visakh16

select * from tablename
where (month >=3 and year=2007)
or (month<=2 and year=2008)


Thank you for the response.
I am using 4 parameters as month1 and year1 & month2 & year2 in dashboard reports.In the following query I will replace for month & year in the where clause with the above parameters.(basically I want the data to be choosen between any month and year to any month and year)

I have 100322 records between March & Dec 2007
and 10134 records in january 2008. Together it should yield 110456
When I use the additional paranthesis( as shown below) after month & year I do get the right number of records. here is the query I used:
SELECT
Count(*)
FROM
inventory_history
where (([Month] >=3and [Year]=2007)
or ([Month]<=1 and Year=2008))
and [Seat_Type] IN ('FULL', 'NO')


But in the above query if I use in the where clause like
where (([Month] >=3 and [Year]=2007)
or ([Month]<=12 and Year=2007)) I do not get the right number of records

can you please help me on this.
Regards,
KKM


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-04 : 12:22:51
you wont because of OR in between. to get records bet ween march to dec 2007 use

where ([Month] >=3 and [month]<=12 and [Year]=2007)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-05 : 07:50:28
Also it is better to have DATETIME datatype and store first day for each month

Madhivanan

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

- Advertisement -