Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9306013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:04:30+00:00 2026-06-19T00:04:30+00:00

Using SQL Server 2008. Let’s say I have 3 numbers to store. First number

  • 0

Using SQL Server 2008.

Let’s say I have 3 numbers to store.

  • First number is any 6bit number
  • Second is any 4bit
  • Third is also any 6bit number

For example:

declare @firstNumber_6bit  tinyint = 50
declare @secondNumber_4bit tinyint = 7
declare @thirdNumber_6bit  tinyint = 63

I need to store those 3 numbers in a 2 byte binary (16bit) variable. So, the binary values for the example are:

  • 50 is 110010,
  • 7 is 0111
  • 63 is 111111
  • The 2 bytes to store are 11001001 11111111

So either one of the following lines should store those values:

declare @my3NumbersIn2Bytes binary(2) = 51711

or

declare @my3NumbersIn2Bytes binary(2) = 0xC9FF

(sorry if I’m messing the byte order in big / little endian, but that’s not the point).

Storing and retriving those numbers is a trivial task with .net CLR, but I’m trying to solve this in pure T-SQL and as we know there is no bit shifting in SQL Server. I saw a lot of examples out there that use memory tables to solve problems like these, but that seems totally overkill for doing a simple bit shift… I was thinking something more like a substring for bits could do the trick. I just want to be sure there’s no other way to solve this before going the overkill way.

So my question is, what is the most effective way to store those 3 numbers and recover them?

Thanks.-

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:04:32+00:00Added an answer on June 19, 2026 at 12:04 am
    declare @firstNumber_6bit  tinyint = 50
    declare @secondNumber_4bit tinyint = 7
    declare @thirdNumber_6bit  tinyint = 63
    
    declare @my3NumbersIn2Bytes binary(2)
    
    select @my3NumbersIn2Bytes = @firstNumber_6bit*1024 + @secondNumber_4bit *64 + @thirdNumber_6bit
    --extract values
    select @firstNumber_6bit = @my3NumbersIn2Bytes/1024
    select @secondNumber_4bit = (@my3NumbersIn2Bytes%1024)/64
    select @thirdNumber_6bit = (@my3NumbersIn2Bytes%1024)%64
    
    select convert(varchar(max), @my3NumbersIn2Bytes, 1) -- just for display
    , @firstNumber_6bit
    , @secondNumber_4bit
    , @thirdNumber_6bit
    

    SQL Fiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008. Let's say I have two hypothetical tables like below:
Using Microsoft SQL Server 2008, let's say there is a table1 that keeps the
(Using SQL Server 2008) I need some help visualizing a solution. Let's say I
I'm working with SQL Server 2008. Let's say I have these three tables: Product
Using SQL Server 2008. I have multiple Locations which each contain multiple Departments which
I am using SQL Server 2008 R2, I have a script to update the
I'm using SQL Server 2008. I have a table with x amount of rows.
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)
Hi i am using SQL Server 2008 R2, i have lots of procedures in
I am using SQL Server 2008 (let us call this source database server in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.