Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Convert integers from decimal to binary display.


Convert integers from decimal to binary display.

Author
Message
David P Reynevich
David P Reynevich
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 36
Comments posted to this topic are about the item Convert integers from decimal to binary display.
ron.mcdowell
ron.mcdowell
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 706
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search