Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use the substr function in SQL

Shulou Source: shulou.com Published: 2022-05-31 20:38:20 12月03日 Update

This article will explain in detail how to use the substr function in SQL. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Substr (string, 1Pol 3) function interpretation: take a 3-word string from left to right in string.

The result is: str

From right to left?

There should be another function to provide this function!

In fact, from right to left, this function is just a difference in parameters.

Substr (String,-1,3) function interpretation: start with 3 digits from the right side of the string.

The result is: G

Substr (string,start,length)

String-the specified string to intercept. Start-required, specifies where to start the string. Positive number-starts at the specified position in the string, negative number-starts at the specified position at the end of the string, 0-starts at the first character in the string. Length-optional, specifies the length of the string to be intercepted, and by default returns all characters before the end of the value of the character expression.

For example: select substr ('abcdefg',3,4) from dual; result is cdef select substr (' abcdefg',-3,4) from dual; result efg

Note: the first position in the string is always 1. The results of the following two sql queries are the same:

For example: select substr ('abcdefg',0,3) from dual; result is abc

Select substr ('abcdefg',1,3) from dual; result is abc

Different from substring in the java class:

Substring (int beginIndex, int endIndex): starting at the specified beginIndex and ending at the specified endIndex-1, the first position in the string is always 0. EndIndex, optional, returns the character from the specified index to the end of the string by default.

For example: "unhappy" .substring (2) returns "happy"

"hamburger" .substring (3pm 8) returns "burge"

On how to use the substr function in SQL to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Characters strings results functions positions contents functions articles more knowledge articles different good same two facts in fact parameters just on the right Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL Apple Redmi Microsoft