+2  A: 

What do you need to do with it?? Do you need to grab the characters after the last occurence of a given delimiter?

If so: reverse the string and search using the normal CHARINDEX:

declare @test varchar(100)
set @test = 'some.file.name'

declare @reversed varchar(100)
set @reversed = REVERSE(@test)

select 
    REVERSE(SUBSTRING(@reversed, CHARINDEX('.', @reversed)+1, 100))

You'll get back "some.file" - the characters up to the last "." in the original file name.

There's no "LASTCHARINDEX" or anything like that in SQL Server directly. What you might consider doing in SQL Server 2005 and up is great a .NET extension library and deploy it as an assembly into SQL Server - T-SQL is not very strong with string manipulation, whereas .NET really is.

marc_s
Hey Marc,Thanks for your reply.I have a string like 'some.file.name'I want to grab 'some.file'.if using LEFT,I need to know the index of the last delimiter.Do you have any good idea to grab 'some.file'?
Shuo
@Shuo: Why not have a go yourself and post your best effort. Then others may be more inclined to assist you.
John Sansom
@Shuo: update my answer for your scenario - should work just fine.
marc_s
@John Sansom:Thanks John!I have revised my question.
Shuo
+1  A: 

This will also work:

DECLARE
     @test     VARCHAR(100)

SET @test = 'some.file.name'

SELECT
     LEFT(@test, LEN(@test) - CHARINDEX('.', REVERSE(@test)))
Tom H.
Tom,we're seeing eye to eye!
Shuo
Heh! After looking through Marc's code and reading all the comments, then getting distracted by something else I totally forgot what your solution had been :)
Tom H.
A: 

Take one ')'

declare @test varchar(100) set @test = 'some.file.name' select left(@test,charindex('.',@test)+charindex('.',@test)-1)

sheeba