易截截图软件、单文件、免安装、纯绿色、仅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


相关文档:

.NET 获取局域网的sql server 列表

SqlDataSourceEnumerator instance =SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = instance.GetDataSources();

System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
返回Datatable
此table包含以下四个字段
Console.WriteLine("服务器名 = {0}", r ......

SQL当前日期获取技巧

SQL当前日期获取技巧
select   getdate()   //2003-11-07   17:21:08.597    
  select   convert(varchar(10),   getdate(),120)   //2003-11-07  
  select   convert(char(8),getdate(),112)  ......

NHibernate执行原始SQL代码的方法小结

在使用NHibernate过程中经常会使用到复杂的sql查询,但是使用hql又比较麻烦的情况下,我们往往都会想到采用原始的sql来执行。但是如何利用NHibernate来执行sql呢?问题来了,在NHibernate中也有AdoTemplate的方法可以执行sql的,但是这里要介绍的是另外一种方法:CreateSQLQuery。以下部分例子源自于网络。
实例一(源自于 ......

SQL Server 语句查询手册

建表:
CREATE TABLE  [DB.dbo].tableName
(Stud_id int CONSTRAINT  constraintName1  not null primary key,
 Name nvarchar(5) not null,
 Birthday datetime,
 Gender nchar(1),
 Telcode char(12),
 Zipcode char(6) CONSTRAINT constraintName2 CHECK(zipcode like [ ......

sql常用语句

在SQLSERVER,简单的组合sp_spaceused和sp_MSforeachtable这两个存储过程,可以方便的统计出用户数据表的大小,包括记录总数和空间占用情况,非常实用,在SqlServer2K和SqlServer2005中都测试通过。
/*
1. exec sp_spaceused '表名'            (SQL统计数据,大量事务操作后可 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号