sql and csharp: Split Function
T-SQL:
declare @int int,@prov int,@city int,@str nvarchar(500)
set @str='天河麗特青春:中國廣東省廣州市天河區天河路623號天河娛樂廣場麗特青春百貨一樓,塗聚文'
select @int=charindex(':',@str)
select @prov=charindex('省',@str)
select @city=charindex('市',@str)
select substring(@str,0,@int)
select substring(@str,@int+1,@prov-(@int))
select substring(@str,@int+1,@city-(@int)) declare @int int,@prov int,@city int,@str nvarchar(500),@branch varchar(20)
select @branch='HDF'
select @int=charindex(':',ShopAddress) from Intranet.dbo.LC where BranchNO=@branch
select @prov=charindex('省',ShopAddress) from Intranet.dbo.LC where BranchNO=@branch
select @city=charindex('市',ShopAddress) from Intranet.dbo.LC where BranchNO=@branch
select substring(ShopAddress,0,@int) from Intranet.dbo.LC where BranchNO=@branch
select substring(ShopAddress,@int+1,@prov-(@int)) from Intranet.dbo.LC where BranchNO=@branch
select substring(ShopAddress,@int+1,@city-(@int)) from Intranet.dbo.LC where BranchNO=@branch select substring(ShopAddress,0,charindex(':',ShopAddress)) from Intranet.dbo.LC ---中國國內分店名稱
select BranchNO+'--'+CompanyName+substring(ShopAddress,0,charindex('市',ShopAddress)+1) from Intranet.dbo.LC select BranchNO,CompanyName,ShopAddress substring(@str,@int+1,@city-(@int)) from Intranet.dbo.LC drop function [dbo].getVipExamBranchName
go
---
create function [dbo].getVipExamBranchName
(
@branch varchar(20),
@key nvarchar(20),
@citykey nvarchar(20)
)
RETURNS NVARCHAR(200)
AS
BEGIN
declare @int int,@prov int,@city int,@str nvarchar(500),@branchcode nvarchar(20),@re NVARCHAR(100),@cityname nvarchar(500)
select @int=charindex(@key,ShopAddress) from Intranet.dbo.LC where BranchNO=@branch--':'
--select @prov=charindex('省',ShopAddress) from Intranet.dbo.LC where BranchNO=@branch
select @city=charindex(@citykey,ShopAddress) from Intranet.dbo.LC where BranchNO=@branch--'市'
select @branchcode=substring(ShopAddress,0,@int) from Intranet.dbo.LC where BranchNO=@branch
--select substring(ShopAddress,@int+1,@prov-(@int)) from Intranet.dbo.LC where BranchNO=@branch
if(@city>@int)
select @cityname=substring(ShopAddress,@int+1,@city-(@int)) from Intranet.dbo.LC where BranchNO=@branch
select @re=@branchcode+'--'+@cityname
RETURN @re
end
GO
select [dbo].getVipExamBranchName ('HDF',':','市') ---函數
CREATE FUNCTION [dbo].[func_Split]
(
@DelimitedString varchar(8000),
@Delimiter varchar(100)
)
RETURNS @tblArray TABLE
(
ElementID int IDENTITY(1,1), -- Array index
Element varchar(1000) -- Array element contents
)
AS
BEGIN -- Local Variable Declarations
-- ---------------------------
DECLARE @Index smallint,
@Start smallint,
@DelSize smallint SET @DelSize = LEN(@Delimiter) -- Loop through source string and add elements to destination table array
-- ----------------------------------------------------------------------
WHILE LEN(@DelimitedString) > 0
BEGIN SET @Index = CHARINDEX(@Delimiter, @DelimitedString) IF @Index = 0
BEGIN INSERT INTO
@tblArray
(Element)
VALUES
(LTRIM(RTRIM(@DelimitedString))) BREAK
END
ELSE
BEGIN INSERT INTO
@tblArray
(Element)
VALUES
(LTRIM(RTRIM(SUBSTRING(@DelimitedString, 1,@Index - 1)))) SET @Start = @Index + @DelSize
SET @DelimitedString = SUBSTRING(@DelimitedString, @Start , LEN(@DelimitedString) - @Start + 1) END
END RETURN
END --測試
DECLARE @SQLStr varchar(100)
SELECT @SQLStr = 'Mickey Mouse, Goofy, Donald Duck, Pluto, Minnie Mouse' SELECT * FROM dbo.func_split(@SQLStr, ',')
csharp:
/// <summary>
/// 分割字符串
/// 塗聚文
///
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public ArrayList getSplit(string str,char key)
{
ArrayList alist = new ArrayList(); string[] sArray = str.Split(key); foreach (string i in sArray)
{ alist.Add(i.ToString());
}
return alist;
}
/// <summary>
/// 正則表達式分割字符串
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public ArrayList getRegexSplit(string str, string key)
{
ArrayList alist = new ArrayList();
string[] resultString = Regex.Split(str, key, RegexOptions.IgnoreCase);
foreach(string i in resultString)
{
alist.Add(i.ToString());
}
return alist;
}
sql and csharp: Split Function的更多相关文章
- SQL自定义函数split分隔字符串
SQL自定义函数split分隔字符串 一.F_Split:分割字符串拆分为数据表 Create FUNCTION [dbo].[F_Split] ( @SplitString nvarchar(max ...
- sql server 自定义split 标值函数
自定义一个函数,分隔一个以分隔符的隔开字符串,例如把'1,3,5,7,9' 变成 数字1 3 5 7 9的结果集. 自定义标值函数: ),)) )) --实现split功能 的函数 as begin ...
- SQL Server 完美SPLIT函数
-- SQL Server Split函数 -- Author:zc_0101 -- 说明: -- 支持分割符多字节 -- 使用方法 -- Select * FROM DBO. ...
- SQL中实现SPLIT函数几种方法
例1 代码如下 复制代码 create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))returns @temp ...
- SQL Server用SPLIT函数分割字符串
declare @str varchar(100),@sql varchar(1000)set @str='1,2,3,4,5,6,7,8,9,10'set @sql='select Value= ...
- SQL语句实现Split并合并查询结果
需求是这样的,需要将数据库中的支付方式列(用";"分割的字符串)按支付方式拆分: 首先参考博客园split的文章,我采用方法2, IF EXISTS ( SELECT * FROM ...
- sql中实现split()功能
http://www.cnblogs.com/yangyy753/archive/2011/11/23/2260618.html 数据库中,总是遇到一些字段内容,想根据某个标识截取一下字符串,可是都想 ...
- sql中的split方法
ALTER function [dbo].[f_split](@SourceSql varchar(8000),@StrSeprate varchar(10))returns @temp table( ...
- sql server 实现split功能 行转列
--實現split功能的函數create function [dbo].[func_split]( @SourceSql varchar(max), @StrSeprate varchar(10))r ...
随机推荐
- linux防火墙(二)—— iptables语法之选项和控制类型
一.语法: iptables [-t 表名] 选项 [链名] [匹配条件] [-j 控制类型] 未指定表名时,默认用filter表:链名,控制类型要大写:除非设置默认策略,否则必须指定匹配条件:不指定 ...
- c语言数据结构学习心得——栈
栈(Stack) 只允许在一端进行插入或删除操作的线性表 栈顶(Top):栈中允许进行插入和删除操作的那一端: 栈底(Bottom):固定的,不允许进行插入或删除的另一端 1.栈是受限的线性表,所以自 ...
- 【问题记录】element is not attached to the page document
遇到ui脚本报错:element is not attached to the page document 解决办法,再次定位即可
- (C/C++) Link List - C++ 版本
利用C++寫一個基本的 Link list 練習,功能包含 pint list.CreatList.Insert.Delete.Reverse.Search.Clear.GetLen. 先建立相關的C ...
- ASP.NET后台取html控件值方式
1.Request.Form[“cbName”]: 可以在后台取到所有为name 为的控件的value值 2.可以通过 把html控件的值付给HiddenField,然后后台调用 3.就是自定义属性 ...
- DDOS压力测试
分布式拒绝服务(DDoS:Distributed Denial of Service)攻击指借助于客户/服务器技术,将多个计算机联合起来作为攻击平台,对一个或多个目标发动DDoS攻击,从而成倍地提高拒 ...
- Hash 日常摸鱼笔记
本篇文章是Hash在信息学竞赛中的应用的学习笔记,分多次更新(已经有很多坑了) 一维递推 首先是Rabin-Karp,对于一个长度为\(m\)的串\(S\) \(f(S)=\sum_{i=1}^{m} ...
- HDU - 4686 函数积的前缀和
题意:求\(\sum_{i=0}^{n-1}a_ib_i\) 其中,\(a_i=A_xa_{i-1}+A_y,b_i=B_xb_{i-1}+B_y\) 构造矩阵分别维护\(a_ib_i,a_i,b_i ...
- HDU 2879 数论
HeHe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submis ...
- C++ GUI Qt4编程(06)-2.3sort
1. 使用Qt设计师创建Sort对话框. 2. sortdialog.cpp /**/ #include "sortdialog.h" SortDialog::SortDialog ...