sqlserver字符串分隔與拼接實(shí)例_第1頁
sqlserver字符串分隔與拼接實(shí)例_第2頁
sqlserver字符串分隔與拼接實(shí)例_第3頁
sqlserver字符串分隔與拼接實(shí)例_第4頁
sqlserver字符串分隔與拼接實(shí)例_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Sql字符串分隔與拼接示例題目:已知A,B兩表 基本數(shù)據(jù)如下:A表(BList是B表的BID集合,用逗號連接):B表:獲得查詢結(jié)果如下:我們的思路如下:創(chuàng)建一個(gè)函數(shù),參數(shù)是IDList,由ID拼接的字符串,返回值是NameList,由Name拼接的字符串。調(diào)用該函數(shù)即可。詳細(xì)的sql語句如下(附帶建表及插入測試數(shù)據(jù)):create table B(BID varchar(3) primary key,BName varchar(20)insert into B(BID,BName) values ('001','蔬菜')insert into B(BID,BNa

2、me) values ('002','水果')insert into B(BID,BName) values ('003','牙膏')insert into B(BID,BName) values ('004','洗發(fā)水')create table A(AID int,BList varchar(4000)-BList是由BID用逗號連接組成的字符串insert into A(AID,BList) values (1,'001,002')insert into A(AID,BList

3、) values (2,'001')insert into A(AID,BList) values (3,'002,003')insert into 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')-

4、我們創(chuàng)建一個(gè)函數(shù)Func_GetNameList。create function Func_GetNameList(IdList varchar(4000)-由逗號拼接 001,002,003 組成的字符串 轉(zhuǎn)換為Name組成的字符串 即 蔬菜,水果,牙膏returns nvarchar(4000)AsBegindeclare resultStr nvarchar(4000)set resultStr=''declare Index intset Index=charindex(',',IdList)if Index is null OR Index=0 -如果

5、不存在逗號(只有一個(gè) 或者 為null)beginselect resultStr=BName from B where BID=IdListreturn resultStrenddeclare BID varchar(3)while Index>0begin set BID=substring(IdList,1,Index-1) if(resultStr='') select resultStr=BName from B where BID=BID else select resultStr=resultStr+','+BName from B wher

6、e BID=BID set IdList=stuff(IdList,1,Index,'') -刪除第一個(gè)逗號前面的字符串 set Index=charindex(',',IdList) if(Index=0) -如果是最后一個(gè) begin select resultStr=resultStr+','+BName from B where BID=IdList break; endendreturn resultStrEnd-查詢Sql語句如下:select AID,dbo.Func_GetNameList(BList) as BListName

7、from A附加一:使用部分字符串函數(shù)charindex、stuff的用法·CHARINDEX()函數(shù)charindex('要搜索的字符串','列名 或 整體全部字符串',查詢起始索引)注:sqlserver中索引從1開始例如:print charindex('abc','dfsaabc')-存在所以返回值為5(abc中的a在dfsaabc里的下標(biāo))charindex函數(shù)的第一個(gè)參數(shù)不能是null 而且 必須是以下類型:char/varchar、nchar/nvarchar 和 binary/varbinary。char

8、index函數(shù)的第二個(gè)參數(shù)可以是null,但結(jié)果返回null當(dāng)?shù)诙€(gè)參數(shù)不是null時(shí)。判斷第一個(gè)字符串是否在第二個(gè)字符串中存在,如果存在,返回第一個(gè)字符串的第一個(gè)字符在第二個(gè)字符串中的下標(biāo)(下標(biāo)從1開始),不存在返回0 charindex函數(shù)的第三個(gè)參數(shù)可以忽略 此時(shí) 按從第一個(gè)字符開始查詢匹配后的索引。charindex函數(shù)的第三個(gè)參數(shù)為null時(shí) 返回nullcharindex函數(shù)的第三個(gè)參數(shù)為負(fù)整數(shù)或0時(shí) 此時(shí) 按從第一個(gè)字符開始查詢匹配后的索引。示例如下:select charindex('a','3abcdsad') -返回2 (從起始字符開始查找)

9、select charindex('a','3abcdsad',null) -返回nullselect charindex('a','3abcdsad',-5) -返回2 (從起始字符開始查找)select charindex('a','3abcdsad',0) -返回2 (從起始字符開始查找)select charindex('a','3abcdsad',4) -返回7 (從第四個(gè)字符開始查找)查詢DocumentSummary字段中包含"bicycle&

10、quot;的所有行。一般大家都會寫成這樣:select * from Production.Documentwhere DocumentSummary like'%bicycle%'了解這個(gè)函數(shù)以后,大家可以這樣寫:select * from Production.Document where charindex('bicycle',DocumentSummary)>0 這種方法比like'%'的形式速度上要快很多.·STUFF() 函數(shù)將一字符串中的某一部分用另一個(gè)字符串替換掉。語法STUFF(原字符串, 開始替換的位置, 被替

11、換的字符數(shù), 用于替換的字符串)·LEFT(),Right()函數(shù)返回某個(gè)被請求部分的左右側(cè)部left('字符串',長度) right('字符串',長度) ·datalength (),len ()函數(shù)取字符串字節(jié)數(shù)用函數(shù)datalength(字符串) 返回任何表達(dá)式所占用的字節(jié)數(shù)。取字符串字符數(shù)用函數(shù)len(字符串) -返回字符數(shù)附加二:sql分隔與拼接函數(shù)-分隔函數(shù)-分隔字符串create function f_splitstr(SourceSql varchar(8000),StrSeprate varchar(100) returns

12、 temp table(F1 varchar(100) as begin declare ch as varchar(100) set SourceSql=SourceSql+StrSeprate while(SourceSql<>'') begin set ch=left(SourceSql,charindex(',',SourceSql,1)-1) insert temp values(ch) set SourceSql=stuff(SourceSql,1,charindex(',',SourceSql,1),'') end return end -測試select * from dbo.f_splitstr('a,b,c,d',',')-合并函數(shù)Create FUNCTION JoinString -合并字符串 多行合并為一行( UserName varchar(50) ) RETURNS varchar(8000) AS BEGIN declare Str varchar(8000) set Str

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論