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 ...
随机推荐
- Python爬取招聘信息,并且存储到MySQL数据库中
前面一篇文章主要讲述,如何通过Python爬取招聘信息,且爬取的日期为前一天的,同时将爬取的内容保存到数据库中:这篇文章主要讲述如何将python文件压缩成exe可执行文件,供后面的操作. 这系列文章 ...
- Python3之os模块
一:简介 os模块主要用于提供系统高级别的操作. 二:常用方法 os.access(path, mode) # 检验权限模式 os.chdir(path) # 改变当前工作目录 os.chflags( ...
- 题解 P1632 点的移动
P1632 点的移动 题目描述 平面上有N个整数坐标点.如果将点(x0,y0)移动到(x1,y1),则需要的代价为|x0-x1|+|y0-y1|.求使得K(K=1,-,N)个点在同一位置上最少需要的代 ...
- Eclipse中文件夹变成包的解决办法(python版)
问题展示如下: 如图,框中的三个文件夹都变成了包的样子. 解决方法如下: 1.在项目文件夹上右键,打开属性框 2.将PYTHONPATH中,Source Folders中的文件夹都删除.即可看到包已变 ...
- redis读取自增时候指定的key问题
首先,此文章是接了如下文章写的 Spring boot redis自增编号控制 踩坑 上面这个问题解决后,公司这边功能其实已经实现了,但是考虑到一种情况,因为我们这边号的生成就是根据上面的自增编号来的 ...
- 014 Android BottomNavigationView 底部导航组件使用
1.导入BottomNavigationView组件(点击下载按钮,安装组件) 2.新建菜单 (1)app--->src-->main--->res ,选中res目录右击new--- ...
- Jupyter 安装与应用
用pip安装Jupyter pip install jupyter 从命令行启动笔记本服务器 jupyter notebook 前提要先启动python,这里有一个 token值,如果不是使用默认浏览 ...
- yyy的python3第七天学习
望着小月亮:https://www.cnblogs.com/triple-y/ 请尊重原创:https://www.cnblogs.com/triple-y/p/9655753.html 第七天学习的 ...
- 洛谷 P4108 / loj 2119 [HEOI2015] 公约数数列 题解【分块】
看样子分块题应该做的还不够. 题目描述 设计一个数据结构. 给定一个正整数数列 \(a_0, a_1, \ldots , a_{n-1}\),你需要支持以下两种操作: MODIFY id x: 将 \ ...
- leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;
,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...