select patindex('%hahaha%',username) from a
结果为1200132但是select substring(username,1200132,50) from a的结果中没有hahaha这种情况不太可能发生!
你再仔细检查一下

解决方案 »

  1.   

    存在这个问题,patindex 对text类型换为ntext 的字段返回的数值是 字符 的位置
    substring 用与 text 的时候 “start 和 length 指定字节的位置”
              用与 nText 的时候 “start 和 length 指定字符的位置”nText是用于unicode 数据的,一个ntext字符的长度为2个字节故用下面的语句能出正确结果
    select substring(username,1200132/2,50) from a
      

  2.   

    bluegift(bluegift) ,除以2也得不到正确的结果的
      

  3.   

    SUBSTRING
    返回字符、binary、text 或 image 表达式的一部分。有关可与该函数一起使用的有效 Microsoft® SQL Server™ 数据类型的更多信息,请参见数据类型。 语法
    SUBSTRING ( expression , start , length ) 参数
    expression是字符串、二进制字符串、text、image、列或包含列的表达式。不要使用包含聚合函数的表达式。start是一个整数,指定子串的开始位置。length是一个整数,指定子串的长度(要返回的字符数或字节数)。说明  由于在 text 数据上使用 SUBSTRING 时 start 和 length 指定字节数,因此 DBCS 数据(如日本汉字)可能导致在结果的开始或结束位置拆分字符。此行为与 READTEXT 处理 DBCS 的方式一致。然而,由于偶而会出现奇怪的结果,建议对 DBCS 字符使用 ntext 而非 text。
    返回类型
    如果 expression 是支持的字符数据类型,则返回字符数据。如果 expression 是支持的 binary 数据类型,则返回二进制数据。返回字符串的类型与给定表达式的类型相同(表中显示的除外)。给定的表达式 返回类型 
    text varchar 
    image varbinary 
    ntext nvarchar 
    注释
    在字符数中必须指定使用 ntext、char 或 varchar 数据类型的偏移量(start 和 length)。在字节数中必须指定使用 text、image、binary 或 varbinary 数据类型的偏移量。 说明  兼容级别可能影响返回值。有关兼容级别的更多信息,请参见 sp_dbcmptlevel。 ---------------
    以上来自sql2000的联机帮助,这里已经说得很清楚了
    >1
    给定的表达式 返回类型 
    text varchar ---------------------最大返回值为8000
    image varbinary 
    ntext nvarchar -------------------最大返回值为4000
    >2
    text 数据以 varchar 的形式返回,这点也说得很清楚
      

  4.   

    >3
    由于在 text 数据上使用 SUBSTRING 时 start 和 length 指定字节数,因此 DBCS 数据(如日本汉字)可能导致在结果的开始或结束位置拆分字符。此行为与 READTEXT 处理 DBCS 的方式一致。然而,由于偶而会出现奇怪的结果,建议对 DBCS 字符使用 ntext 而非 text。
      

  5.   

    >4 联机中的例子
    IF EXISTS (SELECT table_name FROM INFORMATION_SCHEMA.TABLES 
          WHERE table_name = 'npub_info')
       DROP TABLE npub_info
    GO
    -- Create npub_info table in pubs database. Borrowed from instpubs.sql.
    USE pubs
    GO
    CREATE TABLE npub_info
    (
     pub_id         char(4)           NOT NULL
             REFERENCES publishers(pub_id)
             CONSTRAINT UPKCL_npubinfo PRIMARY KEY CLUSTERED,
     pr_info        ntext             NULL
    )GO-- Fill the pr_info column in npub_info with international data.
    RAISERROR('Now at the inserts to pub_info...',0,1)GOINSERT npub_info VALUES('0736', N'üThis is sample text data for New Moon Books, publisher 0736 in the pubs database')
    INSERT npub_info values('0877', N'üThis is sample text data for Binnet & Hardley, publisher 0877 in the pubs databa')
    INSERT npub_info values('1389', N'üThis is sample text data for Algodata Infosystems, publisher 1389 in the pubs da')
    INSERT npub_info values('9952', N'üThis is sample text data for Scootney Books, publisher 9952 in the pubs database')
    INSERT npub_info values('1622', N'üThis is sample text data for Five Lakes Publishing, publisher 1622 in the pubs d')
    INSERT npub_info values('1756', N'üThis is sample text data for Ramona Publishers, publisher 1756 in the pubs datab')
    INSERT npub_info values('9901', N'üThis is sample text data for GGG&G, publisher 9901 in the pubs database. GGG&G i')
    INSERT npub_info values('9999', N'üThis is sample text data for Lucerne Publishing, publisher 9999 in the pubs data')
    GO
    -- Join between npub_info and pub_info on pub_id.
    SELECT pr.pub_id, SUBSTRING(pr.pr_info, 1, 35) AS pr_info,
       SUBSTRING(npr.pr_info, 1, 35) AS npr_info
    FROM pub_info pr INNER JOIN npub_info npr
       ON pr.pub_id = npr.pub_id
    ORDER BY pr.pub_id ASC--result
    0736 This is sample text data for New Mo üThis is sample text data for New M
    0877 This is sample text data for Binnet üThis is sample text data for Binne
    1389 This is sample text data for Algoda üThis is sample text data for Algod
    1622 This is sample text data for Five L üThis is sample text data for Five 
    1756 This is sample text data for Ramona üThis is sample text data for Ramon
    9901 This is sample text data for GGG&G, üThis is sample text data for GGG&G
    9952 This is sample text data for Scootn üThis is sample text data for Scoot
    9999 This is sample text data for Lucern üThis is sample text data for Lucer