Mega Code Archive

 
Categories / MSSQL Tutorial / Data Types
 

The bitwise_and() Scalar Function

3> 4> CREATE FUNCTION dbo.bitwise_and 5> ( 6>   @arg1 varbinary(8), 7>   @arg2 varbinary(8) 8> ) RETURNS varbinary(8) 9> AS 10> BEGIN 11>   DECLARE 12>     @result   AS varbinary(8000), 13>     @numbytes AS int, 14>     @curpos   AS int 15>   SET @result   = 0x 16>   SET @numbytes = DATALENGTH(@arg2) 17>   SET @curpos   = 1 18>   WHILE @curpos <= @numbytes 19>   BEGIN 20>     SELECT 21>       @result = @result + CAST(SUBSTRING(@arg1, @curpos, 1) & 22>                                CAST(SUBSTRING(@arg2, @curpos, 1) AS tinyint) 23>                                AS binary(1)) 24>     SET @curpos = @curpos + 1 25>   END 26>   RETURN @result 27> END 28> GO 1> GRANT EXECUTE ON dbo.bitwise_and TO public 2> GO 1> 2> SELECT dbo.bitwise_and(0x00000001000000010000000100000001, 3>                        0xffffffffffffffffffffffffffffffff) 4> 5> drop function dbo.bitwise_and 6> GO ------------------------------------------------------------------------------------------------------------------------ ---------------------------------- 0x0000000100000001 (1 rows affected) 1>