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
相关文档:
1.说明:复制表(只复制结构,源表名:a 新表名:b)
SQL: select * into b from a where 11
2.说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
SQL: insert into b(a, b, c) select d,e,f from a;
3.说明:显示文章、提交人和最后回复时间
SQL: select a.title,a.username,b.adddate from table a,(select max(adddat ......
select * from pet;
insert into pet values('Liujingwei','Liuchao','cat','f','1984-04-18',null);
UPDATE pet set birth='1989-08-31' WHERE name='Slim';
select * from pet WHERE birth>'1998-1-1';
SELECT * from pet WHEREselect * from pet;
insert into pet values('Liujingwei','Liuchao','cat','f','198 ......
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 ......
在使用NHibernate过程中经常会使用到复杂的sql查询,但是使用hql又比较麻烦的情况下,我们往往都会想到采用原始的sql来执行。但是如何利用NHibernate来执行sql呢?问题来了,在NHibernate中也有AdoTemplate的方法可以执行sql的,但是这里要介绍的是另外一种方法:CreateSQLQuery。以下部分例子源自于网络。
实例一(源自于 ......
1.数据的分布Skip()和take()
Skip(100)代表跳过100行后.从第101行开始分页;
take(10)表示多少行分页;
例:
var result=from pdt in db.Product
select pdt;
this.dataview.DataSource=result.Skip(10).take(10);
......