MS_SQL中从整型中得到4个字节的数值


   看到CSDN上有问的,试着做了一个存储过程来实现它:
   问题描述是这样的:例如整型数16646316,表示为16进制形式则是0XOOFEOOAC,最后需要实现高低位互换然后分解成4个字节数据。

输入:
   0XOOFEOOAC
结果:
   1. OXOO
   2. OXFE
   3. OXOO
   4. OXAC

存储过程实现:
Create   Procedure  p_SplitByteFromInt
@d   int ,
@b1   binary ( 1 ) output,
@b2   binary ( 2 ) output,
@b3   binary ( 3 ) output,
@b4   binary ( 4 ) output
AS
Declare   @t1   smallint , @t2   smallint
Select   @t2   =   CAST ( @d   As   binary ( 2 )), @t1 = CAST ( CAST ( @d   &   0xFFFF0000   As   binary ( 4 ))  As   binary ( 2 ))
Set   @b1   =   CAST ( CAST ( @t1   &   0xFF00   As   binary ( 2 ))  As   binary ( 1 ))
Set   @b2   =   CAST ( @t1   As   binary ( 1 ))
Set   @b3   =   CAST ( CAST ( @t2   &   0xFF00   As   binary ( 2 ))  As   binary ( 1 ))
Set   @b4   =   CAST ( @t2   As   binary ( 1 ))

测试代码:
declare   @b1   binary ( 1 ), @b2   binary ( 1 ), @b3   binary ( 1 ), @b4   binary ( 1 )
exec  p_SplitByteFromInt  16646316 , @b1  output, @b2  output, @b3  output, @b4  output
select   @b1 , @b2 , @b3 , @b4

你可能感兴趣的:(MS_SQL中从整型中得到4个字节的数值)