RiCosNote
Published in

RiCosNote

SQL Server 2022 — 位元操作新函式

/*
RiCo技術學院
https://ricochen-s-school.teachable.com/p/home

SQL Server 2022 - Bit Manipulation

#LEFT_SHIFT、RIGHT_SHIFT、BIT_COUNT、GET_BIT、SET_BIT
#支援Tinyint、Smallint、int、Bigint、binary (n) 和Varbinary (n)
#不支援Linked Server的分散式查詢
#可更有效率計算整數或二進位,透過移動、設定、擷取或計算單一位元
#SQL Server將最左邊視為最大,從右到左進行編號。如:3 等於 00000011
*/

SELECT 32--10_0000
,LEFT_SHIFT(32, 1) --100_0000
,LEFT_SHIFT(32, 2);--1000_0000


SELECT 32--100000
,RIGHT_SHIFT(32, 1) --1_0000
,RIGHT_SHIFT(32, 2);--1000


SELECT BIT_COUNT(32)----10_0000
,BIT_COUNT(31)--1_1111
,BIT_COUNT(cast(1 as smallint))--0001
,BIT_COUNT(cast(2 as smallint))--0010
,BIT_COUNT(cast(-1 as smallint))
,BIT_COUNT(cast(-2 as smallint))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
RiCo 技術農場

分享工作上實戰經驗,如SQL Server,NetCore,C#,WEBApi,Kafka,Azure…等,Architect,Software Engineer, Technical Manger,Writer and Speaker