SQL Server 2005:charindex starting from the end
- by Shuo
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?