MS SQL Server2k字符串分拆與合并處理代碼
--字符串分拆處理代碼
CREATE FUNCTION f_splitSTR(
@s varchar(8000), --待分拆的字符串
@split varchar(10) --數據分隔符
)RETURNS TABLE
AS
--SELECT TOP 8000 ID=IDENTITY(int,1,1) INTO dbo.tb_splitSTR --字符串分拆函數要用到的輔助表.
--FROM syscolumns a,syscolumns b
RETURN(
SELECT col=CAST(SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID) as varchar(100))
FROM tb_splitSTR
WHERE ID<=LEN(@s+'a')
AND CHARINDEX(@split,@split+@s,ID)=ID)
--drop table tb_splitSTR
GO
/* --利用以上函數,對某一表的字符串字段字符分拆處理代碼
declare @id int,@c varchar(50)
declare k cursor for select * from id_f --字符串待分拆原始表(id int,c varchar(50))
open k
fetch next from k into @id,@c
while (@@fetch_status=0)
begin
insert into id_yf --分拆結果表(id int,c varchar(50))
select @id as id,c.* from f_splitSTR(@c,',') as c
fetch next from k into @id,@c
end
close k
deallocate k
*/
----------------------------------------------------------------------------字符串合并處理代碼
declare @s varchar(300)
set @s=''
SELECT @s=@s+c+','
FROM [dbo].[id_h] --字符串待合并表[id int,c varchar(50)]
where id <> (select top 1 id from id_h order by id desc) order by id
select @s=@s+ c from id_h where id =(select top 1 id from id_h order by id desc)
select @s
關鍵字:字符串、代碼、數據
新文章:
- CentOS7下圖形配置網絡的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統后丟失windows啟動項
- CentOS單網卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網打印機IP講解
- CentOS7使用hostapd實現無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網絡重啟出錯
- 解決Centos7雙系統后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統有什么不同呢
- Centos 6.6默認iptable規則詳解