SQL Server 2005:charindex starting from the end

Posted by Shuo on Stack Overflow See other posts from Stack Overflow or by Shuo
Published on 2009-12-05T09:06:35Z Indexed on 2010/04/21 7:53 UTC
Read the original article Hit count: 193

Filed under:
|

Hey,everyone

I have a string 'some.file.name',I want to grab 'some.file'.

To do that,I need to find the last occurrence of '.' in a string.

My solution is :

 declare @someStr varchar(20)

 declare @reversedStr varchar(20)

 declare @index int

 set @someStr = '001.002.003'

 set @reversedStr = reverse(@someStr)

 set @index = len(@someStr) - charindex('.',@reversedStr)

 select left(@someStr,@index)

Well,isn't it too complicated?I was just intented to using 'some.file' in a where-clause.

Anyone has a good idea?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server