SQL检查位标志 [英] SQL check for bit flag

查看:79
本文介绍了SQL检查位标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,应用程序具有已被标记的复选框.根据所选的复选框选项,应用程序会将其作为整数存储在数据库中.如何查询数据库中的int以检查特定位是否处于打开/关闭状态.

So the application has checkbox's that are bit flagged. Depending on what checkbox option is selected, the application will store it as an integer in the database. How can I query the int in the database to check if a specific bit is on/off.

示例:

Decimal   Binary
16        00010000
208       11010000

我想检查第5位是打开还是关闭(或者与此有关的任何东西).

I want to check if the 5th bit is on or off (or any for that matter).

推荐答案

SQL Server没有位移运算符,但类似的方法有效:

SQL Server doesn't have a bit shift operator, but something like this works:

where decimal & power(2,5-1) > 0

当然,使用位图会引出一个问题:您为什么不只使用一组位标志?您可以分别定义每个代码,而且代码更易于阅读.

Of course, using bit maps begs of the question: Why aren't you just using a set of bit flags? You can define each one individually, and the code is much easier to read.

这篇关于SQL检查位标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆