我正在寻找是否有一种方法可以计算一列中尾随零的数量。该列最多由13个数字组成,如下所示,我想在实际数字之前计算所有零,但不确定如何执行此操作。
Example numbers
2200040000000 -- this would have the value 7
1411258181000 -- this would have the value 3
我已经设法在Excel中执行此操作,但是我试图在MySQL中直接在查询中执行此操作,而不是在Excel中导入数据,然后编写查询,因为还有更多步骤需要完成。
以下是公式:
=6+RIGHT(TEXT(B17,"0.##############E+00"),2)-LEN(TEXT(B17,"0.##############E+00"))
如果有人能就如何解决此问题提出建议,那将真的很有益,因为它确实可以帮助我前进,而又不会与Excel来回走动。
答案 0 :(得分:2)
您可以使用string function TRIM()
,该版本自MySQL创立以来就可用:
char_length(num) - char_length(trim(trailing '0' from num))
trim(...)
从字符串中删除结尾的0
;原始值和修剪后的值之间的长度差将为您提供字符串中尾随0
的数量。
create table t (num bigint);
insert into t values (2200040000000), (1411258181000);
select
num,
char_length(num) - char_length(trim(trailing '0' from num)) cnt_trailing_0s
from t;
num | cnt_trailing_0s ------------: | --------------: 2200040000000 | 7 1411258181000 | 3
答案 1 :(得分:1)
您可以使用PartitionKey SortKey Attributes
Building#1 Building#1 (For metadata)
Building#1 Section#1 [...]
Building#1 Section#1|Section#2 [...]
Building#1 Section#1|Section#2|Section#3 [...]
来做到这一点:
reverse()
用列的实际名称替换select col,
length(col) - length(reverse(col) + 0) trailing_zeros
from tablename
。
如果该列的情况仅包含零,则使用以下方法:
col
请参见demo。
结果:
length(col) - length(reverse(col) + 0) + (col = 0)