select top 20 title,substring(Content,1,600) as vContent ,UpdateTime from [Article] where Deleted=0 and Passed=1 order by ArticleId desc
在上述语句中过滤掉“Content”中的HTML,然后取其600个字符。SQL会实现吗?

解决方案 »

  1.   

    select top 20 title,substring(replace(content,'html',''),1,600) as vContent ,UpdateTime from [Article] where Deleted=0 and Passed=1 order by ArticleId desc
      

  2.   

    select top 20 title,substring(replace(content,'html',''),1,600) as vContent ,UpdateTime 
    from [Article] 
    where Deleted=0 and Passed=1 
    order by ArticleId desc
      

  3.   

    这个HTML指的是超文本字符,而不是一个单词
      

  4.   

    sql可以实现,但正则功能有限,建议还是在程序中完成。///   <summary>   
            ///   去除HTML标记   
            ///   </summary>   
            ///   <param   name="NoHTML">包括HTML的源码   </param>   
            ///   <returns>已经去除后的文字</returns>   
            protected string RemoveHTML(string Htmlstring)
            {
                //删除脚本   
                Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
                //删除HTML   
                Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
                Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);            Htmlstring.Replace("<", "");
                Htmlstring.Replace(">", "");
                Htmlstring.Replace("\r\n", "");
                Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();            return Htmlstring;
            }然后在其它方法可以调用它进行处理,甚至直接在页面上处理:<%# RemoveHTML(Eval("Body").ToString()).Length > 600 ? string.Format("{0}...", RemoveHTML(Eval("Body").ToString()).Substring(0, 600)) : RemoveHTML(Eval("Body").ToString()) %>
      

  5.   

    文件头记得引用这个namespace:
    using System.Text.RegularExpressions;
      

  6.   


    谢谢。但出现了: 运算符“>”无法应用于“string”和“int”类型的操作数
    strRSS = strRSS + "<description><![CDATA[" + StripHTML(dt.Rows[i]["vContent"].ToString()).Length> 600 ? string.Format("{0}...", StripHTML(dt.Rows[i]["vContent"].ToString()).Substring(0, 600)) : StripHTML(dt.Rows[i]["vContent"].ToString()) + "]]></description>";
      

  7.   

    我那句话是写在html页面上的,
    你这句写在后台程序里的,不能完全照搬。
    debug一下,看看各个值是否正确。