按条件的查询:我想按条件查询数据库里的信息,其中字段CODE 里保存的七位数字的阿拉伯数字.比如 1100718 /1100788 /1209810 /1351234  ,我想达到的要求是只输入前三位的数字比如 110 即可查询出相应的数据 1100718 /1100788 以下是原语句,任何问题都没有,但不是我想要的模糊查询...........
----------------------------------------------------;
procedure TSarch.BitBtn7Click(Sender: TObject);
var  ss:string;
var mStr1:string;
var low1,high1:string;
begin
   mStr1 := 'select * from information  where ';
   ss := '';   if Rzcombobox6.Text<>'' then
         ss:=ss+'model='''+Rzcombobox6.Text+''' and ';  // 部件代码查询   if Edit7.Text<>'' then
         ss:=ss+'code ='''+Edit7.Text+''' and ';
   if Rzcombobox7.Text<>'' then
         ss:=ss+'shift='''+Rzcombobox7.Text+''' and ';   if Rzcombobox8.Text<>'' then
         ss:=ss+'line='''+Rzcombobox8.Text+''' and ';   if Rzcombobox9.Text<>'' then
         ss:=ss+'grade='''+Rzcombobox9.Text+''' and ';
   low1:=formatdatetime('yyyy-mm-dd',DateTimePicker4.Date);
   high1:=formatdatetime('yyyy-mm-dd',DateTimePicker5.Date);
   ss:=mStr1 + ss + 'workdate<=#'+high1+'# and workdate>=#'+low1+'#' + 'order by id desc' ;  ADOQuery4.Connection:=ADOConnection1;
  ADOQuery4.Close;
  ADOQuery4.SQL.Clear;
  ADOQuery4.SQL.Add(ss);
  ADOQuery4.Open;end;
end.----------------------------------------------------------------
但是我改成下面这样,为什么运行不了? 错在哪里????请指点.....谢谢!!!!!!
...........................................................
  // 部件代码查询   if Edit7.Text<>'' then
         ss:=ss+'code like='''+Edit7.Text+''' and ';..............................................................

解决方案 »

  1.   

    if Edit7.Text <> '' then
      ss := ss + 'code like ''%' + Edit7.Text + '%'' and ';
      

  2.   

    上面适用于SQL Server, 如果是Access, 则:
    if Edit7.Text <> '' then
      ss := ss + 'code like ''*' + Edit7.Text + '*'' and ';
      

  3.   

    模糊查询
    SQL:
       if Edit7.Text <> '' then
    ss := ss + 'code like ''%' + Edit7.Text + '%'' and ';
    Access:
       if Edit7.Text <> '' then
    ss := ss + 'code like ''*' + Edit7.Text + '*'' and ';
      

  4.   

    if Edit7.Text <> '' then
      ss := ss + 'code like ''' + Edit7.Text + '%'' and ';
      

  5.   

    如果Code是整数类型的字段, 则上面给出的修改后的代码是没问题的。如果Code是浮点数类型(比如Float)的字段, 则需要这样写:
    if Edit7.Text <> '' then
      ss := ss + 'CAST(code AS INT) like ''%' + Edit7.Text + '%'' and ';
      

  6.   

    谢谢jadeluo(秀峰) 可惜还是不行诶.
      

  7.   

    谢谢楼上的朋友.if Edit7.Text <> '' then
      ss := ss + 'code like ''' + Edit7.Text + '%'' and ';改成这样,可以了...谢谢!!!!!!
    不好意思,再提个问题.呵,解决问题马上结帖.我现在还想实现 前三位不管.我要查询后四位. 比如***0788 这样的语句怎么写?
      

  8.   

    SQL:
       if Edit7.Text <> '' then
    ss := ss + 'code like '%' + Edit7.Text + '%' and ';
    Access:
       if Edit7.Text <> '' then
    ss := ss + 'code like ''*' + Edit7.Text + '*' and ';
    改成
    SQL:
       if Edit7.Text <> '' then
    ss := ss + 'code like '%' + Edit7.Text + '%' and ';
    Access:
       if Edit7.Text <> '' then
    ss := ss + 'code like '*' + Edit7.Text + '*' and ';
    去掉一个引号试试
      

  9.   

    jiqimiao(jiqimiao) 不好意思.刚刚结帖.才看到你的回答.谢谢了!!!!