我想查询我输入的字符串中的包含的数据,但是必须从开头算起
我不知道该怎么表达 举个例子 看大家能理解不假设A表里有一个 bhbh包括以下五个数据
abc
abcde
bcde
aaa
fgh
我根据字符串abcde查询
查询的结果是 abc 和 abcdebcde是因为不是从第一位开始匹配的
aaa 和 fgh是因为字符串abcde中不包含想知道这样的SQL语句怎么写
我不知道该怎么表达 举个例子 看大家能理解不假设A表里有一个 bhbh包括以下五个数据
abc
abcde
bcde
aaa
fgh
我根据字符串abcde查询
查询的结果是 abc 和 abcdebcde是因为不是从第一位开始匹配的
aaa 和 fgh是因为字符串abcde中不包含想知道这样的SQL语句怎么写
select * from tb where charindex('abcde',bh)=1
create table tb(ar varchar(10))
insert into tb
select 'abc' union all
select 'abcde' union all
select 'bcde' union all
select 'aaa' union all
select 'acf'
godeclare @str varchar(10)
set @str = 'abcde'select ar
from tb
where @str like ar+'%' or ar like @str + '%'drop table tb/*ar
----------
abc
abcde(2 行受影响)
from tb
where @str like ar+'%'
charindex('abcde',‘a’)是等于0的
expression1
Is an expression that contains the sequence of characters to be found. expression1 is an expression of the character string data type category.expression2
Is an expression, typically a column searched for the specified sequence. expression2 is of the character string data type category.
insert into #tb
select 'abc' union all
select 'abcde' union all
select 'bcde' union all
select 'aaa' union all
select 'acf'
go
--SQL:
select * from #tb where charindex(ar,'abcde')=1
/*
abc
abcde
*/
select ckbh from aaa where charindex('abcde',ckbh)=1
select ckbh from aaa where 'abcde' like ckbh+'%' or ckbh like 'abcde' + '%'
select ckbh from aaa where charindex(ckbh,'abcde')=1
这三种都不对啊