易截截图软件、单文件、免安装、纯绿色、仅160KB

sql查找某个字符串第N次出现的位置的函数(转帖)

if exists(select 1 from sysobjects where name='char_index')
drop function char_index
create function char_index(@string varchar(8000),@char varchar(10),@index smallint)
--@string:待查找字符串,@index:查找位置
returns smallint
as
begin
  declare
  @i tinyint,--当前找到第@i个
  @position tinyint--所在位置
  set @position=@index;
  set @i=0;
  while charindex(@char,@string,@position)>0
  begin
    set @position=charindex(@char,@string,@position)+1;
    set @i=@i+1;
    if @i=@index
    begin
     return @position-1;
    end
  end
  return 0;--0表示未找到
end
select dbo.char_index('sdf_dsf_dfgdg_ertr_erte','f_',2)
--如何查找某个字符串第N次出现的位置,
--比如:字符串"sdf_dsf_dfgdg_ertr_erte",要查找"f_"第二次出现的位置
if object_id('f_findstr') is not null
drop function f_findstr
go
create function f_findstr(@s varchar(8000),@find varchar(10),@index int)
returns int
as
begin
declare @startindex int
set @startindex=0
while @index>0
begin
if charindex(@find,@s,@startindex)>0
set @startindex=charindex(@find,@s,@startindex+1)
set @index=@index-1
end
return @startindex
end
go
select dbo.f_findstr('sdf_dsf_dfgdg_ertr_erte','f_',2)
use test
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[F_Str]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[F_Str]
GO
CREATE FUNCTION dbo.F_Str(
@s varchar(8000), 
@pos int,         
@split varchar(10)
)RETURNS int
AS
BEGIN
    IF @s IS NULL RETURN(NULL)
    DECLARE @splitlen int,@i int
    SELECT @splitlen=LEN(@split+'a')-2,@i=0
    WHILE @pos>1 AND CHARINDEX(@split,@s+@split)>0
        SELECT @pos=@pos-1,@i=@i+CHARINDEX(@split,@s+@split)+@splitlen,
&nb


相关文档:

经典SQL语句大全

下列语句部分是Mssql语句,不可以在access中使用。
  SQL分类:
  DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE)
  DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT)
  DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)
  首先,简要介绍基础语句:
  1、说明:创建数据库
......

SQL查询时间返回格式

Select CONVERT(varchar(100), GETDATE(), 23);
返回形式:2008-11-29
Select CONVERT(varchar(100), GETDATE(), 102)  
返回形式:2008.11.29
Select CONVERT(varchar(100), GETDATE(), 101)
返回形式:11/29/2008
更多详情请参见如下列表:
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 ......

sql查询表结构,过程,视图,主键,外键,约束

 一、表结构查询
SELECT TOP (100) PERCENT a.name AS zdm,COLUMNPROPERTY(a.id, a.name, 'IsIdentity') AS bs ,
CASE WHEN EXISTS (SELECT 1 from dbo.sysindexes si INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id
 AND si.indid = sik.indid INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc. ......

SQL基本语句

掌握SQL四条最基本的数据操作语句:Insert,Select,Update和Delete。
   练掌握SQL是数据库用户的宝贵财 富。在本文中,我们将引导你掌握四条最基本的数据操作语句—SQL的核心功能—来依次介绍比较操作符、选择断言以及三值逻辑。当你完成这些学习后,显然你已经开始算是精通SQL了。
  在我们开始之前,先 ......

C#调用SQL存储过程以及返回值和参数的获取

QZone Editor
.dump{}

//TODO 域名
if (location.hash) {
document.domain=location.hash.substr(1);
}

//parent.qZEditor.callback();
function init(){
//获取编辑器面板ID
var pid = frameElement.panelID;

//获取编辑器面板对象 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号