Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In table i have column that is type of varchar(n), and it's not primary/foreign key.
Now, what I need is sql querry that will filter and select all the records in that table like:

(pseudo) select all from table1 where table1.column2 startswith(aa to bz).
aa and bz are first two leters of any record that contains data for that column, and passed as parameters.

First two letters of all selected records then should look like:
aa ab ac ... bd bt bz.

Any suggestions would be appreciated!
Posted
Updated 11-Jan-12 11:13am
v2

This might be one solution
SQL
SELECT SUBSTRING(table1.column2,1,2) as FirstTwoLetters FROM table1
WHERE table1.column2 LIKE 'a%' OR table1.column2 LIKE 'b%' -- ... OR table1.column2 LIKE 'z%'

Here is more about LIKE [^] and SUBSTRING [^] operator and function
 
Share this answer
 
v2
Comments
ZGelic 11-Jan-12 17:19pm    
yes but what if range is greater than from a% to b% for example from a% to t% then output should look like
aa, ab, ac, ... ba, bc, bb... ca, cb, cc..... ta, tb, tc,... tz (first two starting letters)
Wonde Tadesse 11-Jan-12 17:20pm    
That's what the SUBSTRING do. It only selects the first two letters from the column.
ZGelic 11-Jan-12 17:24pm    
your querry would return only ones starting with a and t and not for all the letters in between a and t...
anyway thanks
Wonde Tadesse 11-Jan-12 17:33pm    
It's just adding similar OR conditions from a - z. Basically this can be one solution.See the updated answer.
ZGelic 11-Jan-12 17:49pm    
In that case how many combinations of query exists? And each one should be stored as stored procedure... User can pass any two letters for (select from) and any two letters for (select to). I had in mind something like( I don't know if it is possible):
select * from table1 where SUBSTRING(table1.column2,1,2)<@param1 and SUBSTRING(table1.column2,1,2)>@param2
if someone need solution for this or similar problem...

SQL
select * from table1 where SUBSTRING(table1.column2,1,2)>@param1 and SUBSTRING(table1.column2,1,2)<@param2
 
Share this answer
 
Comments
Wonde Tadesse 12-Jan-12 18:01pm    
It seems that you accomplished your task. 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900