将NVARCHAR与数字分成 int 的最简单方法是什么?例如,来自" 123"的1,2和3。
答案 0 :(得分:9)
假设您希望将其拆分为行'。您可以使用以下代码:
DECLARE @var varchar(10) = 123;
--DECLARE @var varchar(10) = 'charlie';
SELECT SUBSTRING(Temp.input, v.number+1, 1)
FROM (SELECT @var input) Temp
JOIN MASTER..spt_values v ON v.number < LEN(Temp.input)
WHERE v.TYPE = 'P'
输出:
答案 1 :(得分:1)
作为一个注释,您也可以使用递归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;