商品表m_pro
pro_id  pro_name pro_state
  1       电视     110
  2       空调     001
  3       冰箱     101
如果查询到pro_state第一位为1的字符串商品,得到
  1       电视     110
  3       冰箱     101如果查询到pro_state第二位为1的字符串商品,得到
  1       电视     110
如果查询到pro_state第三位为1的字符串商品,得到
  2       空调     001
  3       冰箱     101

解决方案 »

  1.   

    select pro_id, pro_name, pro_state from m_pro where substring(pro_state,1,1) = '1'
      

  2.   

    1.select * from m_pro where left(pro_state,1)='1'
    2.select * from m_pro where substring(pro_state,2,1)='1'
    3.select * from m_pro where right(pro_state,1)='1'
      

  3.   


    declare @t table 
    (pro_id int,pro_name varchar(4),pro_state varchar(3))
    insert into @t
    select 1,'电视','110' union all
    select 2,'空调','001' union all
    select 3,'冰箱','101'declare @i int;set @i=3 --你改这个参数就可以了select * from @t where right(left(pro_state,@i),1)=1
      

  4.   


    use tempdb;
    GO
    /*
    create table m_pro
    (
    pro_id int not null,
    pro_name nvarchar(10) not null,
    pro_state nvarchar(10) not null
    );
    insert into m_pro(pro_id,pro_name,pro_state)
    values
    (1,'电视','110'),
    (2,'空调','001'),
    (3,'冰箱','101');
    */create proc ShowByPos
    @pos int
    as
    set nocount on
    begin
    select * 
    from m_pro
    where right(left(pro_state,@pos),1) = 1
    end
    GOexec ShowByPos 1;
    exec ShowByPos 2;
    exec ShowByPos 3;
      

  5.   

    select * from #E where substring(monryBy,1,1)='1'
    select * from #E where substring(monryBy,2,1)='1'
    select * from #E where substring(monryBy,3,1)='1'
    substring截取字符串