Technical Article

Extract parts of the strings from the SQL results directly by using SQL's substring-extraction API functions

,

Introduction:

If you need to extract a substring or create a longer string by combining multiple strings, there are a few methods you can use. To extract a specific portion of a string, you can utilize a substring-extraction API functions provided by SQL.

On the other hand, if you want to combine strings, you can use CONCAT(). It's possible to extract and display different parts of a string using functions like LEFT(), MID(), and RIGHT(). These functions can extract substrings from the left, middle, or right side of a string.

Command:

SELECT CustomerName, LEFT(CustomerName,2), MID(CustomerName,3,1), RIGHT(CustomerName,3) FROM Customers;

 

Results:

Number of Records: 91

CustomerNameExpr1001Expr1002Expr1003
Alfreds FutterkisteAlfste
Ana Trujillo Emparedados y heladosAnados
Antonio Moreno TaqueríaAntría
Around the HornAroorn
Berglunds snabbköpBerköp
Blauer See DelikatessenBlasen
Blondel père et filsBloils
Bólido Comidas preparadasldas
Bon app'Bon

 

Explanation:

The second parameter in LEFT() and RIGHT() specifies the number of characters to retrieve from the left or right end of the string.

In MID(), the second parameter represents the starting position of the desired substring (starting from 1), while the third parameter indicates the number of characters to retrieve.

SELECT ColumnName, LEFT(ColumnName,2), MID(ColumnName,3,1), RIGHT(ColumnName,3) FROM TableName;

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating