问题描述
在表中,我有varchar(n)
类型的列,它不是主键/外键.
现在,我需要的是sql querry,它将过滤并选择该表中的所有记录,例如:
(伪)从table1
中选择所有内容,其中table1.column2
以(aa到bz)开头.
aa和bz是包含该列数据并作为参数传递的任何记录的前两个字母.
所有选定记录的前两个字母应如下所示:
aa ab ac ... bd bt bz.
任何建议将不胜感激!
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!
推荐答案
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
select * from table1 where SUBSTRING(table1.column2,1,2)>@param1 and SUBSTRING(table1.column2,1,2)<@param2
这篇关于MS-SQL Server查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!