BOL States: CHARINDEX ( expression1 ,expression2 [ , start_location ] ) If either expression1 or expression2 is of a Unicode data type (nvarchar or nchar) and the other is not, the other is converted to a Unicode data type. CHARINDEX cannot be used with text, ntext, and image data types.--> So what about nvarchar(max) or varchar(max) as the input on: expression 1 & 2 ??We have some odd behaviour between expression 1 & 2 on both charindex and patindex:(They are not treated the same)The following has been tested on SQL 2005 : SP3 (Affects both X32 & 64-bit editions)Try the following:-- The following will return error msg: 8152-- Which is search for string ABCDEF (replicated 40000 times)-- and try to find it in string : CDEF-- Should return 0. And no error msg.SELECT PATINDEX ( (REPLICATE(CAST('ABCDEF' AS VARCHAR(MAX)) ,40000)) , 'CDEF')-- The following will return results of 3-- Try to find string: %CDEF% in string-- ABCDEF (replicated 40000 times)-- Should return 3. And no error msg.SELECT PATINDEX ( '%CDEF%', (REPLICATE(CAST('ABCDEF' AS VARCHAR(MAX)) ,40000)) ) -- The following will return error msg: 8152-- Which is search for string ABCDEF (replicated 40000 times)-- and try to find it in string : CDEF-- Should return 0. And no error msg.SELECT CHARINDEX ( (REPLICATE(CAST('ABCDEF' AS VARCHAR(MAX)) ,4000)),'CDEF' ) -- The following will return results of 3-- Try to find string: %CDEF% in string-- ABCDEF (replicated 40000 times)-- Should return 3. And no error msg.SELECT CHARINDEX ( 'CDEF', (REPLICATE(CAST('ABCDEF' AS VARCHAR(MAX)) ,4000)) )
Version
Category
Operating System
Platform
Please wait...