sql >> Base de Datos >  >> RDS >> Sqlserver

Desplazamiento a la derecha sin firmar '>>>' Operador en el servidor sql

T-SQL no tiene operadores de cambio de bits, por lo que tendría que implementar uno usted mismo. Hay una implementación de cambios bit a bit aquí:http ://dataeducation.com/bitmask-handling-part-4-left-shift-and-right-shift/

Tendría que convertir su número entero en un varbinary, usar la función de desplazamiento bit a bit y volver a convertirlo en un número entero y (con suerte) ¡listo! Ahí está el resultado que estás esperando.

La implementación y las pruebas se dejan como un ejercicio para el lector...

Editar:para tratar de aclarar lo que he puesto en los comentarios a continuación, ejecutar este SQL demostrará los diferentes resultados proporcionados por los distintos CAST:

SELECT -5381 AS Signed_Integer,
        cast(-5381 AS varbinary) AS Binary_Representation_of_Signed_Integer,
        cast(cast(-5381 AS bigint) AS varbinary) AS Binary_Representation_of_Signed_Big_Integer, 
        cast(cast(-5381 AS varbinary) AS bigint) AS Signed_Integer_Transposed_onto_Big_Integer, 
        cast(cast(cast(-5381 AS varbinary) AS bigint) AS varbinary) AS Binary_Representation_of_Signed_Integer_Trasposed_onto_Big_Integer

Resultados:

Signed_Integer Binary_Representation_of_Signed_Integer                        Binary_Representation_of_Signed_Big_Integer                    Signed_Integer_Transposed_onto_Big_Integer Binary_Representation_of_Signed_Integer_Trasposed_onto_Big_Integer
-------------- -------------------------------------------------------------- -------------------------------------------------------------- ------------------------------------------ ------------------------------------------------------------------
-5381          0xFFFFEAFB                                                     0xFFFFFFFFFFFFEAFB                                             4294961915                                 0x00000000FFFFEAFB