By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,575 Members | 1,948 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,575 IT Pros & Developers. It's quick & easy.

Give a range in a query

P: 30
hi..

i want to kow wether can we give a range in sql.
maens i want to get the values in field where one period to another period.
example:ther is two fields in the table period and data. i want to get all the values in data field acccording to given period value (like 1 to 3) how can i do this??

thank you in advance
Dec 15 '09 #1

✓ answered by nbiswas

Are you looking for this

Sample data

Expand|Select|Wrap|Line Numbers
  1. declare @t table(period int, data varchar(20))
  2. insert into @t 
  3.     select 1,'data1' union all select 2,'data2' union all 
  4.     select 3,'data3' union all select 4,'data4' union all 
  5.     select 5,'data5' union all select 6,'data6' union all 
  6.     select 7,'data7' union all select 8,'data8' union all 
  7.     select 9,'data9' union all select 10,'data10' 
  8. select * from @t
Query 1:

Expand|Select|Wrap|Line Numbers
  1. select data from @t where period between 1 and 3
Query 2:

Expand|Select|Wrap|Line Numbers
  1. select data from @t where period>= 1 and period<=3
Output:

data
Expand|Select|Wrap|Line Numbers
  1. data1
  2. data2
  3. data3
I am selecting data between the period 1 & 3

Hope this helps.

Share this Question
Share on Google+
2 Replies


nbiswas
100+
P: 149
Are you looking for this

Sample data

Expand|Select|Wrap|Line Numbers
  1. declare @t table(period int, data varchar(20))
  2. insert into @t 
  3.     select 1,'data1' union all select 2,'data2' union all 
  4.     select 3,'data3' union all select 4,'data4' union all 
  5.     select 5,'data5' union all select 6,'data6' union all 
  6.     select 7,'data7' union all select 8,'data8' union all 
  7.     select 9,'data9' union all select 10,'data10' 
  8. select * from @t
Query 1:

Expand|Select|Wrap|Line Numbers
  1. select data from @t where period between 1 and 3
Query 2:

Expand|Select|Wrap|Line Numbers
  1. select data from @t where period>= 1 and period<=3
Output:

data
Expand|Select|Wrap|Line Numbers
  1. data1
  2. data2
  3. data3
I am selecting data between the period 1 & 3

Hope this helps.
Dec 15 '09 #2

P: 30
thank you it worked nicely.
Dec 15 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.