Give a range in a query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buterfly0707
    New Member
    • Nov 2009
    • 30

    Give a range in a query

    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
  • nbiswas
    New Member
    • May 2009
    • 149

    #2
    Solution to Give a range in a query

    Are you looking for this

    Sample data

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

    Code:
    select data from @t where period between 1 and 3
    Query 2:

    Code:
    select data from @t where period>= 1 and period<=3
    Output:

    data
    Code:
    data1
    data2
    data3
    I am selecting data between the period 1 & 3

    Hope this helps.
    Last edited by nbiswas; Dec 15 '09, 08:00 AM. Reason: Added one more way

    Comment

    • buterfly0707
      New Member
      • Nov 2009
      • 30

      #3
      thank you it worked nicely.

      Comment

      Working...