sql server get the position of the last occurrence of a character in a string

0 function:

CHARINDEX ( expression1 , expression2 [ , start_location ] )

Get the position of the first occurrence of a character

 

Parse:

expression1 required --- substring to find

expression2 required --- parent string

start_location optional --- specify the location to search from the parent string, the default location starts from 1

1 function:

REVERSE(express)

Sort string from tail to head

2 functions:

SUBSTR (str, pos, len)

Gets the substring, starting at position <pos> in <str> and selecting the next <len> characters.

3 Combining functions:

Get the position of the last occurrence of the character "." in the string 1.2.3.4

len('1.2.3.4')-CHARINDEX('.',REVERSE('1.2.3.4'))+1

4 Combining functions:

Truncate the substring before the last decimal point

 SUBSTRING('1.2.3.4',1,len('1.2.3.4')- CHARINDEX('.',REVERSE('1.2.3.4')))

The result is as follows:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324942496&siteId=291194637