2009-12-05 7 views
10

J'ai une chaîne 'some.file.name', je veux saisir 'some.file'.SQL Server 2005: charindex à partir de la fin

Pour ce faire, j'ai besoin de trouver la dernière occurrence de '.' dans une chaîne.

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, est-ce pas trop compliqué, je ne faisais que onformément à l'aide de 'some.file' dans une clause where?.

Quelqu'un a une bonne idée?

+0

@Shuo: mettre à jour ma réponse pour votre scénario - devrait fonctionner très bien. –

+0

Merci, Marc! J'ai révisé ma question.Vous êtes très utile! Si quelqu'un ne pouvait pas me donner une meilleure idée, je marquerais votre réponse =) – Shuo

Répondre

16

Qu'avez-vous besoin de faire avec? Avez-vous besoin de saisir les caractères après la dernière occurrence d'un délimiteur donné?

Si oui: inverser la chaîne et la recherche à l'aide CHARINDEX normale: - « »

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'à la dernière dans le nom de fichier 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 chaînes, alors que .NET l'est vraiment.

+0

Hey Marc, Merci pour votre réponse. J'ai une chaîne comme 'some.file.name' Je veux saisir 'some.file'.if en utilisant GAUCHE, j'ai besoin de connaître l'index du dernier délimiteur. Avez-vous une bonne idée de saisir' some. fichier'? – Shuo

+0

@Shuo: Pourquoi ne pas y aller et poster votre meilleur effort. Alors d'autres peuvent être plus enclins à vous aider. –

+0

@Shuo: mettre à jour ma réponse pour votre scénario - devrait fonctionner très bien. –

5

Cela permettra également de travailler:

DECLARE 
    @test  VARCHAR(100) 

SET @test = 'some.file.name' 

SELECT 
    LEFT(@test, LEN(@test) - CHARINDEX('.', REVERSE(@test))) 
+0

Tom, nous voyons les yeux dans les yeux! – Shuo

+0

Heh! Après avoir regardé le code de Marc et lu tous les commentaires, je me suis laissé distraire par autre chose. J'ai complètement oublié ce que ta solution avait été :) –

1

Prenez un ')'

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

Voici une version plus courte

DECLARE @someStr varchar(20) 
set @someStr = '001.002.003' 

SELECT REVERSE(Substring(REVERSE(@someStr),CHARINDEX('.', REVERSE(@someStr))+1,20)) 
1
CREATE FUNCTION [dbo].[Instr] (
    ------------------------------------------------------------------------------------------------- 
    -- Name:  [dbo].[Instr] 
    -- Purpose: Find The Nth Value Within A String 
    ------------------------------------------------------------------------------------------------- 
    -- Revisions: 
    -- 25-FEB-2011 - HESSR - Initial Revision 
    ------------------------------------------------------------------------------------------------- 
    -- Parameters: 
    -- 1) @in_FindString - NVARCHAR(MAX) - INPUT - Input Find String 
    -- 2) @in_String - NVARCHAR(MAX) - INPUT - Input String 
    -- 3) @in_StartPos - SMALLINT - INPUT - Position In The String To Start Looking From 
    --   (If Start Position Is Negative, Search Begins At The End Of The String) 
    --   (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 String Segment (Not Found = 0) 
    ------------------------------------------------------------------------------------------------- 
    @in_FindString    NVARCHAR(MAX), 
    @in_String     NVARCHAR(MAX), 
    @in_StartPos    SMALLINT   = NULL, 
    @in_Nth     SMALLINT   = NULL 
) 
    RETURNS     SMALLINT 
AS 
BEGIN 

    DECLARE @loc_FindString NVARCHAR(MAX); 
    DECLARE @loc_String 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_FindString = @in_FindString; 
    SET @loc_String = @in_String; 

    SET @loc_Nth = ISNULL(ABS(@in_Nth), 1); 
    SET @loc_FindLength = LEN(@loc_FindString+N'.') - 1; 
    SET @loc_Length = LEN(@loc_String+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_FindString,@loc_String,@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_FindString = REVERSE(@in_FindString); 
     SET @loc_String = REVERSE(@in_String); 
     WHILE (@loc_Idx < @loc_Nth) 
     BEGIN 
      SET @loc_Position = CHARINDEX(@loc_FindString,@loc_String,@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 
4

Une façon très simple:

sélectionnez droite (@ str, charindex ('.', Inverse (@str)) - 1)