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 ...
随机推荐
- Trie树【洛谷P3879】 [TJOI2010]阅读理解
P3879 [TJOI2010]阅读理解 题目描述 英语老师留了N篇阅读理解作业,但是每篇英文短文都有很多生词需要查字典,为了节约时间,现在要做个统计,算一算某些生词都在哪几篇短文中出现过. 输入输出 ...
- zbar
源码下载链接:http://sourceforge.net/projects/zbar
- JS 命名规范
JS的命名规则和规范 规则 - 必须遵守的,不遵守会报错 由字母.数字.下划线.$符号组成,不能以数字开头 不能是关键字和保留字,例如:for.while. 区分大小写 规范 - 建议遵守的,不遵守不 ...
- AR资料汇总学习
ARKit 从入门到精通 http://www.chinaar.com/ARKit/5210.html ARKit 框架的学习 http://blog.csdn.net/biangabiang/art ...
- asp.net MVC中的@model与Model
asp.net MVC中的@model与Model https://blog.csdn.net/ydm19891101/article/details/44301201 在MVC的实际使用中,我们经常 ...
- mysql 查询月份数据.
//查看本月数据 SELECT *FROMcontent_publishWHEREdate_format(publish_time, '%Y %m') = date_format(DATE_SUB(c ...
- window平台 php 安装 redis 扩展
1.使用phpinfo() 函数查看PHP的版本信息 <?php phpinfo(); ?> 查看扩展文件版本(特别注意以php版本的 architecture 是x86还是64为准,不能 ...
- Python-append()/extend()
append()向列表尾部添加一个新的元素,只接受一个参数 extend()只接受一个列表作为参数,将参数中的每个元素都添加到原列表 append()用法示例: >> mylist = [ ...
- HDU - 3652 数位DP 套路题
题意:统计能被13整除和含有13的数的个数 解法没法好说的..学了前面两道直接啪出来了 PS.HDU深夜日常维护,没法交题,拿网上的代码随便对拍一下,输出一致 #include<bits/std ...
- 人生苦短,我用pycharm
一.安装 1.首先到官网上下载正版,然后点击安装,只需要更改下面一个地方即可 2.激活码或者帐号的话,可以去百度搜,也可以去某宝买,也就3块钱(一年),不建议使用破解版,如果你真的差这三块钱的话,你还 ...