sqlserver字符串分隔与拼接实例

上传人:油条 文档编号:12458438 上传时间:2017-10-19 格式:DOC 页数:5 大小:171KB
返回 下载 相关 举报
sqlserver字符串分隔与拼接实例_第1页
第1页 / 共5页
sqlserver字符串分隔与拼接实例_第2页
第2页 / 共5页
sqlserver字符串分隔与拼接实例_第3页
第3页 / 共5页
sqlserver字符串分隔与拼接实例_第4页
第4页 / 共5页
sqlserver字符串分隔与拼接实例_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《sqlserver字符串分隔与拼接实例》由会员分享,可在线阅读,更多相关《sqlserver字符串分隔与拼接实例(5页珍藏版)》请在金锄头文库上搜索。

1、Sql 字符串分隔与拼接示例题目:已知 A,B 两表 基本数据如下:A 表(BList 是 B 表的 BID 集合,用逗号连接) :B 表:获得查询结果如下:我们的思路如下:创建一个函数,参数是 IDList,由 ID 拼接的字符串,返回值是 NameList,由 Name 拼接的字符串。调用该函数即可。详细的 sql 语句如下(附带建表及插入测试数据):create table B(BID varchar(3) primary key,BName varchar(20)insert into B(BID,BName) values (001,蔬菜)insert into B(BID,BNam

2、e) values (002,水果)insert into B(BID,BName) values (003,牙膏)insert into B(BID,BName) values (004,洗发水)create table A(AID int,BList varchar(4000)-BList 是由 BID 用逗号连接组成的字符串insert into A(AID,BList) values (1,001,002)insert into A(AID,BList) values (2,001)insert into A(AID,BList) values (3,002,003)insert in

3、to A(AID,BList) values (4,002)insert into A(AID,BList) values (5,001,002,003)insert into A(AID,BList) values (6,002,004)insert into A(AID,BList) values (7,001,002,003,004)-我们创建一个函数 Func_GetNameList。create function Func_GetNameList(IdList varchar(4000)-由逗号拼接 001,002,003 组成的字符串 转换为 Name 组成的字符串 即 蔬菜,水果

4、,牙膏returns nvarchar(4000)AsBegindeclare resultStr nvarchar(4000)set resultStr=declare Index intset Index=charindex(,IdList)if Index is null OR Index=0 -如果不存在逗号(只有一个 或者 为 null)beginselect resultStr=BName from B where BID=IdListreturn resultStrenddeclare BID varchar(3)while Index0beginset BID=substrin

5、g(IdList,1,Index-1)if(resultStr=) select resultStr=BName from B where BID=BIDelseselect resultStr=resultStr+,+BName from B where BID=BIDset IdList=stuff(IdList,1,Index,) -删除第一个逗号前面的字符串set Index=charindex(,IdList)if(Index=0) -如果是最后一个beginselect resultStr=resultStr+,+BName from B where BID=IdListbreak

6、;endendreturn resultStrEnd-查询 Sql 语句如下:select AID,dbo.Func_GetNameList(BList) as BListName from A附加一:使用部分字符串函数 charindex、stuff 的用法CHARINDEX()函数charindex(要搜索的字符串 ,列名 或 整体全部字符串,查询起始索引 )注:sqlserver 中索引从 1 开始例如:print charindex(abc,dfsaabc)-存在所以返回值为 5(abc 中的 a 在 dfsaabc 里的下标)charindex 函数的第一个参数不能是 null 而且

7、 必须是以下类型:char/varchar、nchar/nvarchar 和 binary/varbinary。charindex 函数的第二个参数可以是 null,但结果返回 null当第二个参数不是 null 时。判断第一个字符串是否在第二个字符串中存在,如果存在,返回第一个字符串的第一个字符在第二个字符串中的下标(下标从 1 开始) ,不存在返回 0 charindex 函数的第三个参数可以忽略 此时 按从第一个字符开始查询匹配后的索引。charindex 函数的第三个参数为 null 时 返回 nullcharindex 函数的第三个参数为负整数或 0 时 此时 按从第一个字符开始查询

8、匹配后的索引。示例如下:select charindex(a,3abcdsad) -返回 2 (从起始字符开始查找)select charindex(a,3abcdsad,null) -返回 nullselect charindex(a,3abcdsad,-5) -返回 2 (从起始字符开始查找 )select charindex(a,3abcdsad,0) -返回 2 (从起始字符开始查找)select charindex(a,3abcdsad,4) -返回 7 (从第四个字符开始查找)查询 DocumentSummary 字段中包含bicycle的所有行。一般大家都会写成这样:select

9、 * from Production.Documentwhere DocumentSummary like%bicycle%了解这个函数以后,大家可以这样写:select * from Production.Document where charindex(bicycle,DocumentSummary)0 这种方法比 like%的形式速度上要快很多.STUFF() 函数将一字符串中的某一部分用另一个字符串替换掉。语法STUFF(原字符串, 开始替换的位置, 被替换的字符数, 用于替换的字符串)LEFT(),Right()函数返回某个被请求部分的左右侧部left(字符串 ,长度) right(

10、字符串,长度) datalength (),len ()函数取字符串字节数用函数 datalength(字符串) 返回任何表达式所占用的字节数。取字符串字符数用函数 len(字符串) -返回字符数附加二:sql 分隔与拼接函数-分隔函数-分隔字符串create function f_splitstr(SourceSql varchar(8000),StrSeprate varchar(100) returns temp table(F1 varchar(100) as begin declare ch as varchar(100) set SourceSql=SourceSql+StrSeprate while(SourceSql)set Str=substring(Str,2,len(Str)-1)return Str END

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号