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) sel

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
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 数据库
SQL MIN() Function
SQL MIN() Function
88 6
|
SQL JavaScript 前端开发
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
181 0
|
SQL 关系型数据库 MySQL
SQL编程【MySQL 01】拆分列字段为行并根据类型翻译字段 > 1305 - FUNCTION x.help_topic_id does not exist 报错问题
SQL编程【MySQL 01】拆分列字段为行并根据类型翻译字段 > 1305 - FUNCTION x.help_topic_id does not exist 报错问题
248 0
|
SQL
Greenplum【SQL 03】实现树结构+自定义函数+避免函数重复调用+ function cannot execute on a QE slice 问题处理(优化过程全记录)
Greenplum【SQL 03】实现树结构+自定义函数+避免函数重复调用+ function cannot execute on a QE slice 问题处理(优化过程全记录)
332 0
|
存储 SQL 负载均衡
SQL 函数 function 讲解+代码实例
SQL 函数 function 讲解+代码实例
SQL 函数 function 讲解+代码实例
|
SQL NoSQL 关系型数据库
MySQL:ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA
MySQL:ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA
795 0
|
SQL 数据库 索引
Using Python to Connect Function Compute to SQL Server
Normally, a third-party module is required for establishing Python-based database connections. To connect to Microsoft SQL Server, pymssql is required.
12938 0
|
SQL Go 数据库
SQL Server 自定义函数(Function)——参数默认值
原文:SQL Server 自定义函数(Function)——参数默认值 sql server 自定义函数分为三种类型:标量函数(Scalar Function)、内嵌表值函数(Inline Function)、多声明表值函数(Multi-Statement Function) 标量函数:标量函数是对单一值操作,返回单一值。
2127 0
|
SQL 缓存 Oracle
Oracle On the PL/SQL Function Result Cache
标签 PostgreSQL , Oracle , 函数结果缓存 , 函数三态 , immutable , stable , volatile 背景 Oracle 11g 支持的一个新特性,在创建PL/SQL函数时,可以指定这个存储过程是否需要对结果进行缓存,缓存内容在SGA内存区域。
1042 0
|
SQL Go 数据库
SQL SERVER中用户定义标量函数(scalar user defined function)的性能问题
原文:SQL SERVER中用户定义标量函数(scalar user defined function)的性能问题 用户定义函数(UDF)分类       SQL SERVER中的用户定义函数(User Defined Functions 简称UDF)分为标量函数(Scalar-Valued Function)和表值函数(Table-Valued Function)。
1343 0