Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 计算列中的位数_Sql_Sql Server_Tsql_Count - Fatal编程技术网

Sql 计算列中的位数

Sql 计算列中的位数,sql,sql-server,tsql,count,Sql,Sql Server,Tsql,Count,这是我的密码 select len(cast(code as float)),code from tbl1 where code is not null 这是输出: 我想要代码列中的位数。 我不明白为什么最后一个被计算为12而不是8?将其转换为int: select len(cast(code as int)), code from tbl1 where code is not null; 据推测,某种十进制值正在被计数。将数字的幂10加1。如果ints或reals用于计算整数部分的位

这是我的密码

select len(cast(code as float)),code 
from tbl1
where code is not null
这是输出:

我想要代码列中的位数。
我不明白为什么最后一个被计算为12而不是8?

将其转换为
int

select len(cast(code as int)), code 
from tbl1
where code is not null;

据推测,某种十进制值正在被计数。

将数字的幂10加1。如果ints或reals用于计算整数部分的位数(注意,使用LOG10仅适用于正数,因此我已应用ABS来解决此问题,您的数据可能不需要):


尝试
选择cast(代码为float)
查看它得到的长度。右侧可能有空格吗?实际上加载表时出错。在这种情况下,我应该删除帖子吗?
SELECT code, CASE WHEN Number = 0 THEN 1
                  ELSE FLOOR(LOG10(ABS(code))) + 1 AS NDigits
FROM tbl1