我想查询我输入的字符串中的包含的数据,但是必须从开头算起
我不知道该怎么表达 举个例子 看大家能理解不假设A表里有一个 bhbh包括以下五个数据
abc
abcde
bcde
aaa
fgh
我根据字符串abcde查询
查询的结果是 abc 和 abcdebcde是因为不是从第一位开始匹配的
aaa 和 fgh是因为字符串abcde中不包含想知道这样的SQL语句怎么写 

解决方案 »

  1.   


    select * from tb where charindex('abcde',bh)=1
      

  2.   


    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 行受影响)
      

  3.   

    select ar
    from tb
    where @str like ar+'%' 
      

  4.   

    这个得出的结果有点不全。但我想不出为什么
    charindex('abcde',‘a’)是等于0的
      

  5.   

    CHARINDEX ( expression1 ,expression2 [ , start_location ] ) 
     
    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.
      

  6.   

    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'
    go
    --SQL:
    select * from #tb where charindex(ar,'abcde')=1
    /*
    abc
    abcde
    */
      

  7.   

    select * from tb where charindex(bh,'abcde')=1
      

  8.   


    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
    这三种都不对啊