CREATE FUNCTION dbo.sf_DS_SplitNVarchar
(
@strValues nvarchar(4000)
)
RETURNS @tblStrList TABLE (id int identity(1,1),value nvarchar(4000))
AS
BEGIN
declare @strTmp nvarchar(4000)
declare @intPos int
select @strValues=ltrim(rtrim(@strValues))

while @strValues<>''
begin
select @intPos=case when charindex(',',@strValues)=0 then len(@strValues) else charindex(',',@strValues)-1 end
select @strTmp=ltrim(rtrim(substring(@strValues,1,@intPos)))
select @strValues=ltrim(rtrim(substring(@strValues,@intPos+2,len(@strValues))))

insert into @tblStrList values(@strTmp)
end

RETURN
END

-- select value from dbo.sf_ds_splitnvarchar('A,B,C')

--【方法二】

CREATE FUNCTION dbo.sf_SplitVarchar_Sign
(
@strValues varchar(8000), --待分割字符串
@Sign char(1) --分割符
)
RETURNS @tblStrList TABLE (Zid int identity(1,1),value varchar(8000))
AS
BEGIN

declare @strTmp varchar(8000),
@intPos int

select @strValues=ltrim(rtrim(@strValues))
if right(@strValues,1) <> @Sign
select @strValues = @strValues + @Sign

if @strValues <> @Sign
begin
select @intPos = charindex(@Sign,@strValues)
while @intPos > 0
begin
select @strTmp = ltrim(rtrim(substring(@strValues,1,@intPos - 1)))
insert into @tblStrList values(@strTmp)
select @strValues = ltrim(rtrim(substring(@strValues,@intPos + 1,len(@strValues))))
select @intPos = charindex(@Sign,@strValues)
select @intPos = isnull(@intPos,0)
end
end

RETURN
END

--select value from dbo.sf_SplitVarchar_Sign('A,B,C',',')

--A
--B
--C

SQL Server 分割字符串转列的更多相关文章

  1. SQL Server 分割字符串和合并多条数据为一行

    分割字符串函数 create function f_split(@c varchar(2000),@split varchar(2)) returns @t table(col varchar(20) ...

  2. Sql Server分割字符串函数

    -- Description: 分割字符串函数 -- SELECT * FROM dbo.Split('a,b,c,d,e,f,g',',') -- ========================= ...

  3. SQL Server 分隔字符串函数实现

    在SQL Server中有时候也会遇到字符串进行分隔的需求.平时工作中常常遇到这样的需求,例如:人员数据表和人员爱好数据表,一条人员记录可以多多人员爱好记录,而往往人员和人员爱好在界面展示层要一并提交 ...

  4. [转] SQL SERVER拼接字符串(字符串中有变量)

    本文转自:http://blog.csdn.net/sikaiyuan2008/article/details/7848926 SQL SERVER拼接字符串(字符串中有变量)对我来说是一个难点,总是 ...

  5. SQL Server自动化运维系列——关于邮件通知那点事(.Net开发人员的福利)

    需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的检测等.如果发生异常,需要提前预警的,通知形式一般为发邮件告知. 邮件作为一种非常便利的预警实现方式,在及时性和易用性 ...

  6. SQL Server自动化运维系列——监控跑批Job运行状态(Power Shell)

    需求描述 在我们的生产环境中,大部分情况下需要有自己的运维体制,包括自己健康状态的检测等.如果发生异常,需要提前预警的,通知形式一般为发邮件告知. 在上一篇文章中已经分析了SQL SERVER中关于邮 ...

  7. SQL Server:字符串函数

    以下所有例子均Studnet表为例: 1. len():计算字符串长度 len()用来计算字符串的长度,每个中文汉字或英文字母都为一个长度 select sname, len(sname) from ...

  8. SQL SERVER将某一列字段中的某个值替换为其他的值 分类: MSSQL 2014-11-05 13:11 67人阅读 评论(0) 收藏

    SQL SERVER将某一列字段中的某个值替换为其他的值 UPDATE 表名 SET 列名 = REPLACE(列名 ,'贷','袋') SQL SERVER"函数 replace 的参数 ...

  9. .NET SQL Server连接字符串句法

    .NET SQL Server连接字符串句法 数据库的连接性已经发展成为应用程序开发的一个标准方面.数据库连接字符串现在已经成为每个项目的标准必备条件.我发现自己为了找到所需要的句法,经常要从另外一个 ...

随机推荐

  1. 关于CommandTimeOut

    指示在终止尝试和产生错误之前执行命令期间需等待的时间. 设置和返回值 设置或返回长整型值,该值指示等待命令执行的时间(单位为秒).默认值为 30. 说明 Connection 对象或Command 上 ...

  2. 我的C++笔记(类与对象)

    /* * Main.cpp * * Created on: 2015-7-24 * Author: feiruo */ /* * 类与对象: * * 1.抽象: * 面向对象方法中的抽象,是指对具体问 ...

  3. 点击之后上传图片到页面 input type="file" 样式

    <!DOCTYPE html><head> <meta http-equiv="Content-Type" content="text/ht ...

  4. Django01 web http 基础

    一.内容回顾 1.python基础 2.网络编程 3.并发编程 4.前端 5.数据库(MySQL) 二.今日概要 1.了解Web应用程序的本质 2.Django简介及安装使用 三.今日详细 1.最简单 ...

  5. Substring Uva 11468_记忆化搜索 + AC自动机

    Code: #include<cstdio> #include<cstring> #include<queue> using namespace std; cons ...

  6. 洛谷P3628 [APIO2010]特别行动队 斜率优化

    裸题,注意队列下标不要写错 Code: #include<cstdio> #include<algorithm> #include<cmath> using nam ...

  7. Codeforces 787B Not Afraid( 水 )

    链接:传送门 题意:判断 m 组数,如果某一组中出现负数就判断这一组中是否存在与之相反的数,如果每一组中都满足要求则输出 "NO" 反之输出 "YES" 思路: ...

  8. Spring学习总结(17)——Spring AOP权限管理

    每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...

  9. 教你十分钟构建好 SpringBoot + SSM 框架

    目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵活目前受到了许多人的青睐.而 SpringBoot 的轻量化,简化项目配置, 没有 XML 配置要求等优点现在也得到了 ...

  10. D - Constructing Roads

    D - Constructing Roads 思路:并查集板子 #include<cstdio> #include<cstring> #include<iostream& ...