Home >> FAQs/Tutorials >> SQL Server FAQ

SQL Server FAQ - Converting Binary Strings into Integers

By: FYIcenter.com

(Continued from previous topic...)

How To Convert Binary Strings into Integers?

Binary strings and integers are convertible implicitly and explicitly. But there several rules you need to remember:

  • Binary strings will be implicitly converted into an integer data type, if it is involved in an arithmetical operation with another integer data type operand.
  • Binary strings will be implicitly converted into an integer data type, if it is assigned to a variable, a column or a parameter of an integer data type.
  • Binary strings will be explicitly converted into integer data types using CAST() and CONVERT() functions.
  • When converting binary strings that have more bytes than the target data type size, bytes on the left hand side will be truncated.
  • When converting binary strings that have less bytes than the target data type size, 0x00 will be padded on the left hand side.

Examples showing in the tutorial exercise below will help you remembering those rules.

SELECT 0x66 + 44
GO
146

DECLARE @integer INT;
SET @integer = 0x66;
SELECT @integer + 44
GO
146

SELECT CAST(0x66 AS INT) + 44
GO
146

SELECT CONVERT(INT, 0x66) + 44
GO
146

-- Only last 4 bytes are used for INT conversion
SELECT 0x7700000066 + 44
GO
146

-- 8 bytes will be used for BIGINT conversion
SELECT 0x7700000066 + CONVERT(BIGINT,44)
GO
511101108370

-- 0x00 padded on the left hand side
SELECT 0x66 + 44
SELECT 0x00000066 + 44
GO
146
146

(Continued on next topic...)

  1. How To Concatenate Two Character Strings Together?
  2. What Happens When Unicode Strings Concatenate with Non-Unicode Strings?
  3. How To Convert a Unicode Strings to Non-Unicode Strings?
  4. What Are the Character String Functions Supported by SQL Server 2005?
  5. How To Insert New Line Characters into Strings?
  6. How To Locate and Take Substrings with CHARINDEX() and SUBSTRING() Functions?
  7. How To Concatenate Two Binary Strings Together?
  8. Can Binary Strings Be Used in Arithmetical Operations?
  9. How To Convert Binary Strings into Integers?
  10. Can Binary Strings Be Converted into NUMERIC or FLOAT Data Types?
  11. Can Binary Strings Be Converted into Character Strings?
  12. Can Binary Strings Be Converted into Unicode Character Strings?
  13. How To Convert Binary Strings into Hexadecimal Character Strings
  14. What Are Bitwise Operations?

Related Articles:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...