Solved

Missing If statement using Bitwise operators

Posted on 2009-05-04
3
322 Views
Last Modified: 2012-05-06
Hi all you experts I am trying to convert a number to a true/false statement. True if the bit is on.

Below is my data:
Ident  Device                    Shelf   Row          count bit
15        TubeStore0113      10             2               7

I want to convert 7 into position 1,2,3 are on and position 4 is off based on the bit value.
Below I & the bit with the column 1-4 and got a return of 1,2,4,8 which is correct, but I guess I am having trouble changing that value to true/false.

So how would I add an If statement around each column and retrun true if the value > 0
SELECT     TOP (1) Ident, DeviceName, Shelf, Row, CoumnCount, CountBit, TubeCount, CountBit & 1 AS Column1, CountBit & 2 AS Column2, 

                      CountBit & 4 AS Column3, CountBit & 8 AS Column4

FROM         dbo.TempBitCountByShelf

WHERE     (DeviceName = N'TubeStore0113') AND (CoumnCount = 3)

ORDER BY Ident

Open in new window

0
Comment
Question by:TraciShultz
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 500 total points
ID: 24295775
Do you mean something like this?
 

SELECT ...

	CASE WHEN CountBit & 1 > 1 THEN 'TRUE' ELSE 'FALSE' END AS Column1, 

	CASE WHEN CountBit & 2 > 1 THEN 'TRUE' ELSE 'FALSE' END AS Column2, 

	CASE WHEN CountBit & 4 > 1 THEN 'TRUE' ELSE 'FALSE' END AS Column3, 

	CASE WHEN CountBit & 8 > 1 THEN 'TRUE' ELSE 'FALSE' END AS Column4

FROM ...

Open in new window

0
 

Author Closing Comment

by:TraciShultz
ID: 31577571
Excellent solution.
0
 

Author Comment

by:TraciShultz
ID: 24295859
Final Solution:
SELECT     TOP (100) Ident, DeviceName, Shelf, Row, CoumnCount, CountBit, TubeCount,
                      CASE WHEN CountBit & 1 > 0 THEN 'TRUE' ELSE 'FALSE' END AS Column1, CASE WHEN CountBit & 2 > 0 THEN 'TRUE' ELSE 'FALSE' END AS Column2,
                       CASE WHEN CountBit & 4 > 0 THEN 'TRUE' ELSE 'FALSE' END AS Column3,
                      CASE WHEN CountBit & 8 > 0 THEN 'TRUE' ELSE 'FALSE' END AS Column4
FROM         dbo.TempBitCountByShelf
WHERE     (DeviceName = N'TubeStore0113')
ORDER BY Ident
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now