MS SQL Server: Last Index Of / lastIndexOf / Get last Index Of

Problem

There is no SQL Function to get the last index of a string.

Ansatz – Approach

The first two lines only declare a variable myString with content SER-SOP-12 to Test the combined SQL Functions.
The SELECT-Clause returns the lastIndex of (can be used in standard MS SQL Statements)

Lösung – Solution

Just copy it to the MS SQL Server Management Studio in a query window to test and press F5:

DECLARE @myString AS varchar(255)
SET @myString ='SER-SOP-12'
SELECT LEN(@myString )-CHARINDEX('-', REVERSE(@myString )) 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.