Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Convert integers from decimal to binary display. Expand / Collapse
Author
Message
Posted Monday, October 8, 2007 2:12 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 3, 2013 11:50 AM
Points: 20, Visits: 36
Comments posted to this topic are about the item Convert integers from decimal to binary display.
Post #407886
Posted Wednesday, May 18, 2011 7:48 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 7:28 AM
Points: 14, Visits: 608
Here is the result of my Convert-Loop-To-Tally exercise:

/* Convert signed integer to binary notation: */

DECLARE @BINval CHAR(39) = '' -- Format: 'xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx'
,@INTval INT = 2147483647 -- Positive limit
--,@INTval INT = -2147483648 -- Negative limit

SELECT @BINval = CASE WHEN @INTval & power(2, N) > 0 THEN '1' ELSE '0' END /* Determine bit value */
+ CASE WHEN N%4 = 0 THEN ' ' ELSE '' END /* Blks of 4 bits each */
+ @BINval
FROM [dbo].[Tally] WITH (NOLOCK)
WHERE N BETWEEN 0 AND 30 /* Set all but sign bit */
--ORDER BY N /* My tally tbl PK is N */

-- Add sign bit
SELECT @BINval = CASE WHEN @INTval >= 0 THEN '0' ELSE '1' END
+ @BINval

-- Voila...
SELECT @BINval

Post #1110978
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse