函数要return单元格中的任意字符串值按长度到新行吗?
Function to return any string value in a cell to a new row according to the length of it?
现在我有这个 table:
Names
----------
Bill
James
----------
我的目的是根据它们的长度将这些名称重复插入到新行中。
例如
Names
----------
Bill
Bill
Bill
Bill
James
James
James
James
James
P.S。我将找出是否有一种方法可以只使用 where
、having
...等过滤器来处理它。
提前致谢。
您可以使用递归 CTE:
with cte as (
select name, len(name) as n
from t
union all
select name, n - 1
from cte
where n > 1
)
select cte.name
from cte;
Here 是一个 db<>fiddle.
最简单、最有效的方法是使用 numbers/tally table
模拟计数 table,这里称为 digits
- 理想情况下,这将是一个永久性 table 存储从 1 到 bajillion 的数字,或任何您需要的数字。
with digits as (
select * from (values (1),(2),(3),(4),(5))v(n)
),
T as (
select 'Bill' [Name] union all select 'James'
)
select t.[Name]
from T
cross apply digits d
where d.n <= Len(T.[name])
现在我有这个 table:
Names
----------
Bill
James
----------
我的目的是根据它们的长度将这些名称重复插入到新行中。
例如
Names
----------
Bill
Bill
Bill
Bill
James
James
James
James
James
P.S。我将找出是否有一种方法可以只使用 where
、having
...等过滤器来处理它。
提前致谢。
您可以使用递归 CTE:
with cte as (
select name, len(name) as n
from t
union all
select name, n - 1
from cte
where n > 1
)
select cte.name
from cte;
Here 是一个 db<>fiddle.
最简单、最有效的方法是使用 numbers/tally table
模拟计数 table,这里称为 digits
- 理想情况下,这将是一个永久性 table 存储从 1 到 bajillion 的数字,或任何您需要的数字。
with digits as (
select * from (values (1),(2),(3),(4),(5))v(n)
),
T as (
select 'Bill' [Name] union all select 'James'
)
select t.[Name]
from T
cross apply digits d
where d.n <= Len(T.[name])