you just need to use SUBSTRINGSELECT SUBSTRING(mc, 1,20) as mc from yourtableSUBSTRING works on text/ntext too

解决方案 »

  1.   

    string s="……";
    s=s.length>20?s.Substring(0,20):s;
      

  2.   

    但我在text类型搜出来的结果中出现了一些乱字符,该如何处理呢
      

  3.   

    if you are storing Chinese, you should be using ntext
      

  4.   

    public class aa
    {
        public static string substring(object obj_str,int len)
        {
            if(obj_str.ToString.Trim().Length>len)
            {
                 obj_str.ToString()=obj_str.ToString().SubString(0,len);
            }
            return obj_str.ToString().Trim();
        }
    }aa.substring(object,20)
    可以有条件地截取字段的长度
      

  5.   

    select mc = case when len(mc)<20 then mc else substring(mc,1,20) end  from table
      

  6.   


    可以用存储过程
    this.command.type="stroreproc……";
    this.command.text="ReturnString";
    this.command.paramm…….add(……);SQL Server 过程
    create proc ReturnString
        @cString varchar(20)
    as 
    begin
         select @cString = mc from yourtable where ……
    end
    go
    返回字符多少可以自己定义 @cString varchar(20) 的大小。
      

  7.   

    有点错误,改为:
    SQL Server 过程
    create proc ReturnString
        @cString varchar(20) output
    as 
    begin
         select @cString = mc from yourtable where ……
    end
    go

    SQL Server 过程
    create proc ReturnString
    as 
    begin
         select left(mc ,20) from yourtable where ……
    end
    go