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

CSV文件导入到SQL Server 2005数据库中

首先用查询语句,从tblTask表中查询出所有的数据,然后将其保存为csv格式。
在SQL语句窗口,输入如下内容:
USE Keii BULK INSERT      dbo.tblTask
                      & ......

动态sql语句基本语法

1 :普通SQL语句可以用Exec执行
      例:      Select * from tableName
                Exec('select * from tableName')
        & ......

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事务处理


 
BEGIN TRANSACTION--开始事务
DECLARE @errorSun INT --定义错误计数器
SET @errorSun=0 --没错为0
UPDATE a SET id=232 WHERE a=1 --事务操作SQL语句
SET @errorSun=@errorSun+@@ERROR --累计是否有错
UPDATE aa SET id=2 WHERE a=1 --事务操作SQL语句
SET @errorSun=@errorSun+@@ERROR --累计是否有错
I ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号