T-SQL CHARINDEX Returns 0 Incorrectly

March 5, 2009 14:25 by nat

I was tearing my hair out recently (I didn't have much hair left in the first place, which makes it all the worse!) when the CHARINDEX function was returning 0 even though the string I was looking for definitely existed in the input string!

The database column being searched was of type nText and it turns out that the function concatenates the column value (at what length, I don't know), unless you CAST it to a more usable data type.

So, I added a CAST([ColumnName] AS nVarChar(max)) to the script and hey presto it starts working!!

I would be very appreciative of a message or something to let me know that the input string was concatenated.

Hope this saves someone else from tearing some of their precious hair out!

Nat


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

June 18. 2009 21:17

I have just used this idea to execute a stored procedure. However, I used convert(nvarchar(xx), var) in the same situation as you. Thanks for the insight.

Martin Barron

September 26. 2009 09:52


I have just used this idea to execute a stored procedure. However, I used convert(nvarchar(xx), var) in the same situation as you. Thanks for the insight.

Tiffany Bracelets

Add comment


 

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 31. 2010 20:13

Calendar

July 2010
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567