用户登录
用户注册

分享至

从字符串 TSQL 中获取数字

  • 作者: 感觉今天自己好神经
  • 来源: 51数据库
  • 2022-11-16

问题描述

将带有数字的 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;
软件
前端设计
程序设计
Java相关