SQL Server 2005: charindex à partir de la fin

J'ai une string 'some.file.name', je veux saisir 'some.file'.

Pour ce faire, j'ai besoin de find la dernière occurrence de '.' dans une string.

Ma solution est:

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) 

Eh bien, n'est-ce pas trop compliqué? J'avais juste l'intention d'utiliser 'some.file' dans une clause where.

Quelqu'un a une bonne idée?

Qu'est-ce que tu as besoin de faire avec ça? Avez-vous besoin de saisir les caractères après la dernière occurrence d'un délimiteur donné?

Si oui: inversez la string et effectuez une search en utilisant le CHARINDEX normal:

 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)) 

Vous récupérerez "some.file" – les caractères jusqu'au dernier "." dans le nom de file d'origine.

Il n'y a pas de "LASTCHARINDEX" ou quelque chose comme ça dans SQL Server directement. Ce que vous pourriez faire dans SQL Server 2005 et plus est une excellente bibliothèque d'extension .NET et déployez-la en tant qu'assemblage dans SQL Server – T-SQL n'est pas très fort avec la manipulation de strings, alors que .NET l'est vraiment.

Cela fonctionnera également:

 DECLARE @test VARCHAR(100) SET @test = 'some.file.name' SELECT LEFT(@test, LEN(@test) - CHARINDEX('.', REVERSE(@test))) 

Un moyen très simple est:

select right (@ str, charindex ('.', reverse (@str)) – 1)

Prends-en un ')'

 declare @test varchar(100) set @test = 'some.file.name' select left(@test,charindex('.',@test)+charindex('.',@test)-1) 
 CREATE FUNCTION [dbo].[Instr] ( ------------------------------------------------------------------------------------------------- -- Name: [dbo].[Instr] -- Purpose: Find The Nth Value Within A Ssortingng ------------------------------------------------------------------------------------------------- -- Revisions: -- 25-FEB-2011 - HESSR - Initial Revision ------------------------------------------------------------------------------------------------- -- Parameters: -- 1) @in_FindSsortingng - NVARCHAR(MAX) - INPUT - Input Find Ssortingng -- 2) @in_Ssortingng - NVARCHAR(MAX) - INPUT - Input Ssortingng -- 3) @in_StartPos - SMALLINT - INPUT - Position In The Ssortingng To Start Looking From -- (If Start Position Is Negative, Search Begins At The End Of The Ssortingng) -- (Negative 1 Starts At End Position 1, Negative 3 Starts At End Position Minus 2) -- 4) @in_Nth - SMALLINT - INPUT - Nth Occurrence To Find The Location For ------------------------------------------------------------------------------------------------- -- Returns: SMALLINT - Position Of Ssortingng Segment (Not Found = 0) ------------------------------------------------------------------------------------------------- @in_FindSsortingng NVARCHAR(MAX), @in_Ssortingng NVARCHAR(MAX), @in_StartPos SMALLINT = NULL, @in_Nth SMALLINT = NULL ) RETURNS SMALLINT AS BEGIN DECLARE @loc_FindSsortingng NVARCHAR(MAX); DECLARE @loc_Ssortingng NVARCHAR(MAX); DECLARE @loc_Position SMALLINT; DECLARE @loc_StartPos SMALLINT; DECLARE @loc_Nth SMALLINT; DECLARE @loc_Idx SMALLINT; DECLARE @loc_FindLength SMALLINT; DECLARE @loc_Length SMALLINT; SET @loc_FindSsortingng = @in_FindSsortingng; SET @loc_Ssortingng = @in_Ssortingng; SET @loc_Nth = ISNULL(ABS(@in_Nth), 1); SET @loc_FindLength = LEN(@loc_FindSsortingng+N'.') - 1; SET @loc_Length = LEN(@loc_Ssortingng+N'.') - 1; SET @loc_StartPos = ISNULL(@in_StartPos, 1); SET @loc_Idx = 0; IF (@loc_StartPos = ABS(@loc_StartPos)) BEGIN WHILE (@loc_Idx < @loc_Nth) BEGIN SET @loc_Position = CHARINDEX(@loc_FindSsortingng,@loc_Ssortingng,@loc_StartPos); IF (@loc_Position > 0) SET @loc_StartPos = @loc_Position + @loc_FindLength ELSE SET @loc_Idx = @loc_Nth; SET @loc_Idx = @loc_Idx + 1; END; END ELSE BEGIN SET @loc_StartPos = ABS(@loc_StartPos); SET @loc_FindSsortingng = REVERSE(@in_FindSsortingng); SET @loc_Ssortingng = REVERSE(@in_Ssortingng); WHILE (@loc_Idx < @loc_Nth) BEGIN SET @loc_Position = CHARINDEX(@loc_FindSsortingng,@loc_Ssortingng,@loc_StartPos); IF (@loc_Position > 0) SET @loc_StartPos = @loc_Position + @loc_FindLength ELSE SET @loc_Idx = @loc_Nth; SET @loc_Idx = @loc_Idx + 1; END; IF (@loc_Position > 0) SET @loc_Position = @loc_Length - @loc_Position + (1 - @loc_FindLength) + 1; END; RETURN (@loc_Position); END; GO 

Voici une version plus courte

 DECLARE @someStr varchar(20) set @someStr = '001.002.003' SELECT REVERSE(Subssortingng(REVERSE(@someStr),CHARINDEX('.', REVERSE(@someStr))+1,20))