仅选择2的幂 [英] SELECT only powers of 2
本文介绍了仅选择2的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表中包含一个包含整数的列。我正在寻找一种方法来只选择此列中整数幂为2的那些行,例如1,2,4,8,16,32,64等。
谢谢
I have a column in a table that contains integers. I am looking for a way to select only those rows that have integer powers of 2 in this column, such as 1, 2, 4, 8, 16, 32, 64, etc.
Thanks
推荐答案
您没有提到数据库,但如果您使用的是Sql Server 2012,那么一种方法是使用日志 [ ^ ]功能。
例如:
You didn''t mention the database but if you''re using Sql Server 2012 then one way is to use LOG[^] function.
For example:
SELECT ...
FROM SomeTable
WHERE LOG(SomeField, 2) = ROUND(LOG(SomeField, 2), 0)
这应该返回字段包含2的幂数的所有行。
在获得2的权力的更多解释Sql Server 2012 [ ^ ]
您可以使用此如何判断数字是否为2的幂 - 数字 [ ^ ]。
谢谢Mika!
我是用sql server 2008版本做的登录。起初我得到了一个类型转换错误,但我最终修复它(不知何故)。
Thanks Mika!
I did it using the sql server 2008 version of log. At first I was getting a type conversion error, but I ended up fixing it (somehow).
WHERE (LOG(ServiceMask) / LOG(2)) != CONVERT(int, (LOG(ServiceMask) / LOG(2)))
我发现了很难LOG在这里真正意味着LN的方式。所以我转换成了2号基地。
I found out the hard way that LOG really means LN here. So I converted to base 2.
这篇关于仅选择2的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文