从字符串 TSQL 中获取数字

2021-09-10 00:00:00 sql tsql sql-server

将带有数字的 NVARCHAR 拆分为 int 的最简单方法是什么?例如123"中的 1、2 和 3.

What is the easiest way to split NVARCHAR with numbers into int's? For example 1, 2 and 3 from "123".

推荐答案

注意,您也可以使用递归 CTE 来做到这一点:

As a note, you can also do this with a recursive CTE:

with val as (
      select '123' as val
     ),
     cte as (
      select left(val, 1) as c, substring(val, 2, len(val)) as rest
      from val
      union all
      select  left(rest, 1), substring(rest, 2, len(rest))
      from cte
      where rest <> ''
     )
select c
from cte;

相关文章