Reputation: 648
In SQL Server, shift operators are not present as per knowledge.
If I have to achieve right shift and left shift, what will be the efficient way of doing it?
OR
Please suggest which one will be the more efficient way of doing it.
Upvotes: 4
Views: 8403
Reputation: 453648
From SQL Server 2022 you can simply use
LEFT_SHIFT ( expression_value, shift_amount )
(or <<
)RIGHT_SHIFT ( expression_value, shift_amount )
(or >>
)Upvotes: 5
Reputation: 8725
Option 1 of course.
with mathematical expressions which will give me the same output as shift operator
A single left shift is just a multiplication by 2. repeat as often as necessary to emulate a << n
.
Accordingly, right shift is integer division by 2.
There is also a nice hack involving varbinary
, described in this related answer.
Even with arithmetic overflow prevention added, either will outperform an unmanaged/managed transition in SQL server's CLR hosting environment by a magnitude.
Upvotes: 3