对于输入的二进制bContent参数所占字节数小于iLen/8的部分,在后面补0函数实现如下,在调试时发现,mysql中 bContent + 0x00 被当成的值计算。如下函数功能在mysql中如何实现,谢谢
drop function if exists GetContentByFieldLen;
delimiter //
create function GetContentByFieldLen( bContent VARBINARY(100), iLen BIGINT)
returns VARBINARY(100)
MODifIES SQL DATA
begin
    DECLARE iTemp bigint;       SET iTemp = length(bContent);
    
    while iTemp < @iLen/8 do
       set bContent = bContent + 0x00;
       set iTemp = iTemp + 1;
    end while;
    
    return bContent ;
      
end;
//
delimiter ; 
感觉MYSQL对VARBINARY类型的操作没有SQL SERVER强大。请高手们看看有没有办法,谢谢

解决方案 »

  1.   

    改成如下。MySQL中没有专门的连接运算符。set bContent = concat(bContent , 0x00);
      

  2.   

    用concat可以在补0,但是和我想要的效果不太一样。例如:输入0x452,希望得到0x4520
    如果用concat在后面补0就变成了0x045200
      

  3.   

    需求举例如下:
    如果输入的值长度是奇数位,输出时需要让其在后面补0MYSQL 中对VARBINARY参数长度为奇数时自动再前面补0,我现在想要的是在后面补0.举例:输入0X452时,输出0X4520
      

  4.   

    你输入的时候是怎么输入的?请说明。select GetContentByFieldLen( (0x452 , 100); ?
    建议贴出你的测试方法,另外第二个参数想实现什么? 如果是 :输入0X452时,输出0X4520 ,似乎不需要第二个参数。
      

  5.   

    现在的要求就是 :输入0X452时,输出0X4520 ,不需要第二个参数。如下:
    select GetContentByFieldLen(0x452);  返回的结果是0x4520
    select GetContentByFieldLen(0x4521);  返回的结果是0x4521即输入参数长度是奇数位的返回结果时在后面补0,偶数位时返回原值.
      

  6.   

    mysql> set @x = 0x452;
    Query OK, 0 rows affected (0.00 sec)mysql> select unhex(if(left(hex(@x),1)='0',concat(mid(hex(@x),2),'0'),hex(@x)));
    +-------------------------------------------------------------------+
    | unhex(if(left(hex(@x),1)='0',concat(mid(hex(@x),2),'0'),hex(@x))) |
    +-------------------------------------------------------------------+
    | E                                                                 |
    +-------------------------------------------------------------------+
    1 row in set (0.00 sec)mysql> select hex(unhex(if(left(hex(@x),1)='0',concat(mid(hex(@x),2),'0'),hex(@x))));
    +------------------------------------------------------------------------+
    | hex(unhex(if(left(hex(@x),1)='0',concat(mid(hex(@x),2),'0'),hex(@x)))) |
    +------------------------------------------------------------------------+
    | 4520                                                                   |
    +------------------------------------------------------------------------+
    1 row in set (0.00 sec)mysql>
    mysql> set @x = 0x4521;
    Query OK, 0 rows affected (0.00 sec)mysql> select unhex(if(left(hex(@x),1)='0',concat(mid(hex(@x),2),'0'),hex(@x)));
    +-------------------------------------------------------------------+
    | unhex(if(left(hex(@x),1)='0',concat(mid(hex(@x),2),'0'),hex(@x))) |
    +-------------------------------------------------------------------+
    | E!                                                                |
    +-------------------------------------------------------------------+
    1 row in set (0.00 sec)mysql> select hex(unhex(if(left(hex(@x),1)='0',concat(mid(hex(@x),2),'0'),hex(@x))));
    +------------------------------------------------------------------------+
    | hex(unhex(if(left(hex(@x),1)='0',concat(mid(hex(@x),2),'0'),hex(@x)))) |
    +------------------------------------------------------------------------+
    | 4521                                                                   |
    +------------------------------------------------------------------------+
    1 row in set (0.00 sec)mysql>你的 结帖率:0.00%