1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

How do I implement LastIndexOf functionality in SQLServer 2000?

Discussion in 'SQL - Questions and Answers' started by sharad, Sep 12, 2006.

  1. sharad Guest

    Dialect: SQL 92
  2. Dimitar Guest

    Dialect: MS SQL
    SQL:

    select case when charindex('12',s) =0 then 0 else length(s)-charindex(reverse('12'),reverse(s)) end as lastIndexOf, s from t;

    The above was not tested.
    The above finds the last index of '12' in column 's' of table 't'.
    Hope this helps.
  3. Adam Guest

  4. Dimitar Guest

    Adam,

    The solution you posted works only when the string to search consists of exactly one character.
    When a longer string is needed you need to reverse both, as in my answer #1.

    Don't you agree?

    Dimitar

Share This Page