Search code examples
sqlsql-serversql-server-2014

Select values between two columns range


I have a table like this:

i1   i2
----------
1    a
1    b
1    c
1    d
2    x
3    y
4    a
4    b
4    c

I want to select the rows between 1 c and 4 a.
The result should be:

1 c
1 d 
2 x 
3 y 
4 a

How can I do this?


Solution

  • I would do this as:

    select t.*
    from t
    where (i1 > 1 or (i1 = 1 and i2 >= 'c')) and
          (i1 < 4 or (i1 = 4 and i2 <= 'a'));