table里为varchar型的某个字段,按一个按钮将其为数字与字符串分开,用Delphi该如何写?如table:ID Description
1 abc
2 123
4 xyz将Description字段中的abc和xyz放一起, 而123放另一个地方.
请大家给我一点思路.谢谢!

解决方案 »

  1.   

    不知楼主的"放另一个地方"是什么意思,其实,直接用SQL就可实现你的功能(与数据库有关)
    给你个mysql的例子(其它数据库可作适当修改)
    1.建表:
    create table t3(id integer,Description varchar(10));
    2.输入测试数据:
    insert into t3 values(1,'abc');
    insert into t3 values(2,'123');
    insert into t3 values(3,'xyz');
    3.用SQL得到结果:
    select id,case cast(Description as UNSIGNED) when 0 then Description else  '' end as c , case cast(Description as UNSIGNED) when 0 then 0  else Description end as n from t3;
    结果:+------+------+------+
    | id   | c    | n    |
    +------+------+------+
    |    1 | abc  | 0    |
    |    2 |      | 123  |
    |    3 | xyz  | 0    |
    +------+------+------+
    数字/字符分别在两个字段中.当然,你可单独取数字或字符
    这样,在delphi中可直接得到要的结果了
    如果,只单独取字符或数字,可用:
    字符:
     select id, Description from t3 where cast(Description as UNSIGNED)=0;
    +------+-------------+
    | id   | Description |
    +------+-------------+
    |    1 | abc         |
    |    3 | xyz         |
    +------+-------------+
    数字:
    select id, Description from t3 where cast(Description as UNSIGNED)<>0;
    +------+-------------+
    | id   | Description |
    +------+-------------+
    |    2 | 123         |
    +------+-------------+
      

  2.   

    --建立测试环境
    Create Table 表(IDDescription varchar(10))
    --插入数据
    insert into 表
    select '1abc' union
    select '2123' union
    select '345' union
    select '2134523' union
    select '4512' union
    select '12' union
    select '34125' union
    select '4xyz' union
    select 'ddfgdf' union
    select 'dfg' union
    select 'sdfgdfdfg'
    select * from 表
    --测试语句
     select * from 表 where ISNUMERIC(IDDescription)=0IDDescription 
    ------------- 
    1abc
    4xyz
    ddfgdf
    dfg
    sdfgdfdfg(所影响的行数为 5 行)
    select * from 表 where ISNUMERIC(IDDescription)=1IDDescription 
    ------------- 
    12
    2123
    2134523
    34125
    345
    4512(所影响的行数为 6 行)
     
    --删除测试环境
    Drop Table 表
      

  3.   

    总结select * from 表 where ISNUMERIC(IDDescription)=0
    select * from 表 where ISNUMERIC(IDDescription)=1
      

  4.   

    楼上都说了这些用SQL语句可以实现,用Delphi也可以实现
    ADOQuery.SQL.Text := 'select * from 表';
    ADOQuery.Open;var
      I: Integer;with ADOQuery do
    begin
      while not Eof do
      begin
        if TryStrToInt(FieldByName('Description').Value, I) then//TryStrToInt64也可
        begin
          //这儿的Description是数字
        end else
        begin
          //这儿的Description不是数字
        end;
        Next;
      end;
    end;