34,873
社区成员
发帖
与我相关
我的任务
分享
len
4
5
12
146
574
1980
2413
3342
49824
select [len],len(ltrim([len])) from tbif object_id('[tb]') is not null drop table [tb]
go
create table [tb]([len] int)
insert [tb]
select 4 union all
select 5 union all
select 12 union all
select 146 union all
select 574 union all
select 1980 union all
select 2413 union all
select 3342 union all
select 49824
select [len],count(distinct num) as cnt
from
(
select [len],substring(ltrim([len]),b.number,1) as num
from [tb] a
join master..spt_values b on b.type='P'
where b.number between 1 and len([len])
) t
group by [len]
--测试结果:
/*
len cnt
----------- -----------
4 1
5 1
12 2
146 3
574 3
1980 4
2413 4
3342 3
49824 4
(所影响的行数为 9 行)
*/
create table #tb([len] int)
insert #tb
select 4 union all
select 5 union all
select 12 union all
select 146 union all
select 574 union all
select 1980 union all
select 2413 union all
select 3342 union all
select 49824
select [len],len(ltrim([len])) as strLen from #tb
select len([len]) from tbselect [len],len([len]) from tb
select len(len)