Results 1 to 6 of 6

Thread: Charindex function

  1. #1
    Join Date
    May 2009
    Posts
    41

    Charindex function

    The question is regarding charindex function!!

    DECLARE @document varchar(64)

    SELECT @document = 'Reflectors are vital safety' +
    ' components of your bicycle.'
    SELECT CHARINDEX('vital', @document, 5)
    GO

    In the above statement what does 5 do for me!!
    i read it is the starting point of the search!! but it does not make any difference whether i have 5 there or not!!
    The result will be 16 with 5 or without 5!!
    Pls help!!
    Thanks!!!

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Try 'SELECT CHARINDEX('Ref', @document, 5)', you'll see the difference.

  3. #3
    Join Date
    May 2009
    Posts
    41
    Thanks 4 the reply!!!

    But my question was regarding the no.5 (the start location parameter)
    my question was

    no matter i enter 5 or i dont!!! the result it is giving is always 16 for the query that i wrote!!!

    if i dont enter 5 it should start the search from the begining and if i enter 5 thre the earch should begin from the 5th character!!! right????

  4. #4
    Join Date
    Apr 2009
    Posts
    86
    yogesphu, I haven't looked up the CHARINDEX function but from what I see in this post, it returns the position number of the character string you are looking for. Since VITAL is at position 16, the function will return 16 if you enter 5, or any number between 1 and 16.

    But I think the answer to your question is that if you leave the 5 off completely, it probably defaults to a 1 and starts the search from the beginning of the string.

  5. #5
    Join Date
    Sep 2002
    Posts
    5,938
    That's right, start searching position will not affect string start position.

  6. #6
    Join Date
    May 2009
    Posts
    41
    got u
    thanks guys!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •