日期:2014-05-18  浏览次数:20485 次

(难度极高的问题)在sql server数据库中截取特定字符()
在sqlserver2000数据库中,如何截取news表中conten字段内容如下:
"现在的孩子编造之风太可怕了!”昨日,家住大渡口的张先生带着儿子,急切要求参加本报与课堂内外杂志社联办的“创新作文夏令营”,改变孩子编作文的怪现状。
<IMG alt="" src="http://localhost:1339/system_dntb/upload/1.gif">"


本人想截取"/system_dntb/upload/1.gif"字符窜,请问如何做呀

------解决方案--------------------
select content from news where content like %gif%

string aa;
aa = "现在的孩子编造之风太可怕了!”昨日,家住大渡口的张先生带着儿子,急切要求参加本报与课堂内外杂志社联办的“创新作文夏令营”,改变孩子编作文的怪现状。 <IMG alt='' src='http://localhost:1339/system_dntb/upload/1.gif'>";
int B=aa.IndexOf("system_dntb/upload/");
int E = aa.IndexOf(".gif");
Label1.Text = aa.Substring(B, E - B+4);
------解决方案--------------------
随便写了点,稍微改进下就能够解决楼主的问题了,我要睡觉了...........

SQL code
declare @url varchar(1024)
declare @rightindex int --从右边开始第几个/的内容
declare @index int

set @url = ' http://localhost:1339/system_dntb/upload/1.gif ' -- 输入的参数
set @rightindex = 1 --比如说我想取得右边开始第1个/后的内容,那么就填1

-- 这里开始是业务逻辑
set @url = reverse(ltrim(rtrim(@url)))
set @index = 1
while @index < len(@url)
    begin
        if left(reverse(left(@url,@index)),1) = '/'
            begin
                set @rightindex = @rightindex - 1
                if @rightindex = 0 print reverse(left(@url,@index))
            end
        set @index = @index + 1
    end