有一张表:
table(col1,col2,col3)
其中col3的值为六位二进制数据,其中每一位对应不同的值
1--A
2--B
3--C
4--D
5--E
6--F
比如:110011表示为:ABEF   111000表示为:ABC
即,每一位二进制数,为1时表示为对应的字母,为0时不表示该位的值。
如何把col3的值转换为ABCDEF这种方式的表示,col1 和col2的值不变,
把数据集表示在dbGrid中?

解决方案 »

  1.   

    SQLServerselect col3 = (case substring(col3 ,1,1) when  '1' then 'A' else '' end) +
                  (case substring(col3 ,2,1) when  '1' then 'B' else '' end) +
                  (case substring(col3 ,3,1) when  '1' then 'C' else '' end) +
                  (case substring(col3 ,4,1) when  '1' then 'D' else '' end) +
                  (case substring(col3 ,5,1) when  '1' then 'E' else '' end) +
                  (case substring(col3 ,6,1) when  '1' then 'F' else '' end)
      from tablename   
      

  2.   

    function TForm1.change(s: string): string;
    var i:integer;
        R:string;
        p:string;
    begin
       p:='ABCDEF';
       R:='';
       for i:=1 to length(s)  do
       begin
          if copy(s,i,1)='1' then
             begin
               R:=R+copy(p,i,1);
             end;
       end;
       result:=R;
    end;
      

  3.   

    调用上面的函数例:
       s:='110011';
       R:=change(s);