Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Check the bits of a numeric field

Is it possible to check the bits of a numeric field in the WHERE condition of a SQL request? The only means that I found, for example if I want to test the bit 0, is:

```SELECT * FROM MyTable
WHERE NumField IN (1, 3, 5,...)```

Is there another solution?

Yes, what you could do is convert the number to a string representation of its binary value.

In MySQL, there's a very handy function called CONV that will do this:

```create table numbers ( numfield integer );

insert into numbers (numfield) values (0);
insert into numbers (numfield) values (1);
insert into numbers (numfield) values (2);
insert into numbers (numfield) values (3);
insert into numbers (numfield) values (937);
insert into numbers (numfield) values (1234567);

select numfield
, conv(numfield,10,2) as bitstr
from numbers

numfield  bitstr
0  0
1  1
2  10
3  11
937  1110101001
1234567  100101101011010000111```

Now all that's left to do is decide which bit position you want to test. If it's the right-most position, you can use the RIGHT function:

```select numfield
, conv(numfield,10,2) as bitstr
from numbers
where right(conv(numfield,10,2),1) = '0'

numfield  bitstr
0  0
2  10 ```

However, if it's any other position, you will have to figure out the position by using the LENGTH function and counting backwards. Complicating this is the fact that the string might be shorter than you expect, so you'll have to concatenate enough zeroes on to the front of it. For example, to test whether the bit in the eighth position from the right is on, you have start at the end, which is given by the LENGTH function, and them move over 7 positions to the left:

```select numfield
, conv(numfield,10,2) as bitstr
from numbers
where mid('0000000'+conv(numfield,10,2)
,length('0000000'+conv(numfield,10,2))-7
,1) = '1'

numfield  bitstr
937  1110101001
1234567  100101101011010000111```

Other database systems may not have conversion functions exactly like MySQL's CONV. Look for CAST or CONVERT, and check the documentation to see whether you can convert numeric to binary and then binary to character string.

One final note: if you are indeed checking the rightmost bit position, as in your example, then you could also just divide by 2 and see if there's a remainder:

```select numfield
from numbers
where mod(numfield,2) = 1

numfield
1
3
937
1234567```

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### MariaDB X4 brings smart transactions to open source database

Open source database vendor MariaDB updates its flagship platform with new features to enable the convergence of transactional ...

• ### How data lineage tools boost data governance policies

Organizations can bolster data governance efforts by tracking the lineage of data in their systems. Get advice on how to do so ...

• ### Proactive practices for data quality improvement

Instead of waiting for data quality to become an issue, consider a proactive approach. Here are some ways to improve data quality...

• ### Storytelling using data makes information easy to digest

In a Q&A, Nate Nichols and Anna Schena Walsh of AI-based analytics vendor Narrative Science talk about how data storytelling can ...

• ### Data-driven storytelling makes data accessible

In a Q&A, Nate Nichols and Anna Schena Walsh of data storytelling vendor Narrative Science discuss their book on storytelling and...

• ### Startup Uplevel targets software engineering efficiency

Startup vendor Uplevel emerged from stealth on Wednesday touting a platform aimed at improving the efficiency of software ...

## SearchSAP

• ### SAP S/4HANA migration: Critical advice for moving off ECC

With the end of SAP ECC support looming in 2025, organizations must make some tough decisions. Here's a look at your choices.

• ### New SAP leadership faces big challenges in 2020

Industry analysts discuss SAP's biggest issues in 2020, including how the two new CEOs will guide the company deeper into the ...

• ### SAP Data Hub opens predictive possibilities at Paul Hartmann

When medical supply firm Paul Hartmann AG tested a supply chain analysis system built on SAP Data Hub, it found that it could ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Don't ever put a non-Java LTS release into production

Development teams should avoid non-long-term support releases at all costs. Pay attention to the Java release cycle to make sure ...

• ### Public API strategy considerations for enterprise adoption

As organizations look for more cost-effective ways to manage data, an evolving landscape with APIs has made the technology more ...

• ### Ideas on how to hold a successful code hackathon

Want to host a hackathon? Here are some ideas on what a company can do to host an event that solves problems and reenergizes the ...

## SearchDataCenter

• ### Organizations try to predict the effect of 5G infrastructure

With more 5G and IoT devices emerging, admins must prepare their data centers to support low-latency apps and edge computing with...

• ### Top infrastructure and operations technology myths of 2019

Admins are consistently evaluating technology to improve I&O efficiency. Cost, integration and business goals are key components ...

• ### Improve efficiency with server energy consumption tools

Deciding what servers are most efficient for your infrastructure requires research. Hardware-level certifications and web-based ...

## SearchContentManagement

• ### Ex-SAP exec steers Episerver CMS toward digital experience market

Alex Atzberger discusses leaving the helm of SAP's CX platform to become Episerver CEO. Now, Episerver looks to reinvent itself ...

• ### 4 digital transformation terms to know

As technology evolves, businesses have no choice but to follow. Digital transformations are inevitable in order to keep customers...

• ### How to differentiate between CX and DX platforms

CX and DX platforms have similarities in how they manage content to produce interactive experiences, but they aren't the same. ...

## SearchHRSoftware

• ### Top 7 HR trends for 2020 and beyond: The change decade

Because HR requires a balance between the needs of the organization with the needs of employees, it's always been a balancing act...

• ### For insider threat programs, HR should provide checks and balances

Insider threat programs may backfire if employees feel they are intrusive and violate privacy, Forrester Research warns. Making ...

• ### Better HR security could help thwart Iranian cyberattack

HR departments have a broad role to help protect their firms from Iranian cyberattacks. The security efforts include helping with...

Close