查看: 5910|回复: 0
打印 上一主题 下一主题

在SQL Server数据库中拆分字符串函数

[复制链接]
跳转到指定楼层
1#
发表于 2008-11-17 14:02:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
SQL Server数据库中拆分字符串函数的具体方法:

以下为引用的内容:
  CREATE  FUNCTION uf_StrSplit '1.1.2.50','.'
  (@origStr varchar(7000),  --待拆分的字符串
  @markStr varchar(100))  --拆分标记,如','
  RETURNS @splittable table
  (
   str_id  varchar(4000) NOT NULL, --编号ID
   string  varchar(2000) NOT NULL --拆分后的字符串
  )
  AS
  BEGIN
  declare @strlen int,@postion int,@start int,@sublen int,
  @TEMPstr varchar(200),@TEMPid int
  SELECT @strlen=LEN(@origStr),@start=1,@sublen=0,@postion=1,
  @TEMPstr='',@TEMPid=0
  if(RIGHT(@origStr,1)<>@markStr )
  begin
  set @origStr = @origStr + @markStr
  end
  WHILE((@postion<=@strlen) and (@postion !=0))
  BEGIN
  IF(CHARINDEX(@markStr,@origStr,@postion)!=0)
  BEGIN
  SET @sublen=CHARINDEX(@markStr,@origStr,@postion)-@postion;
  END
  ELSE
  BEGIN
  SET @sublen=@strlen-@postion+1;
  END
  IF(@postion<=@strlen)
  BEGIN
  SET @TEMPid=@TEMPid+1;
  SET @TEMPstr=SUBSTRING(@origStr,@postion,@sublen);
  INSERT INTO @splittable(str_id,string)
  values(@TEMPid,@TEMPstr)
  IF(CHARINDEX(@markStr,@origStr,@postion)!=0)
  BEGIN
  SET @postion=CHARINDEX(@markStr,@origStr,@postion)+1
  END
  ELSE
  BEGIN
  SET @postion=@postion+1
  END
  END
  END
  RETURN
  END  


例如:select * from uf_StrSplit('1,1,2,50',',')

输出结果:

以下为引用的内容:
  str_id   string
  1       1
  2       1
  3       2
  4      50
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表