SQL - SUBSTRING() Function



The SQL SUBSTRING() function is used to retrieve the substring(part of a string) from a string.

It accepts three parameters expression, start, and length, and returns a part of a character, binary, text, or image expression in the SQL server.

If the start parameter value is 1, it indicates the first character of the expression, and if the start value is length-1 meaning that the last character.

Note − The values for start and length parameters must be specified in the number of characters for example ntext, char, or varchar data types, and bytes used for the text, image, binary, or varbinary data types.

Syntax

Following is the syntax of the SQL SUBSTRING() function −

SUBSTRING ( expression ,start , length )  

Parameters

  • expression − It is a character, binary, text, ntext, and image expression.

  • start − It is an integer(or bigint) value that specifies where the returned string starts

  • length − It is a positive integer that specifies the length of the returned string.

Return value

This function returns the part of the character.

Example

In the following example,we are using the SQL SUBSTRING() function to retrieve the part of the character at the start position 2, and specified length 5, from the expression ‘Hello World’.

SELECT SUBSTRING('Hello World', 2, 5) AS NEW_STRING;

Output

The above SQL query produces the following output −

+------------+
| NEW_STRING |
+------------+
| ello       |
+------------+

Example

If we pass the start argument value as negative value to the function.

Following is another example of the SUBSTRING() function, if the start argument value is less than 1, the returned expression will begin at the first character that is specified in the expression. In this case, the number of characters that are returned is the largest value of either the sum of start + length- 1 or 0.

SELECT SUBSTRING('TutorialsPoint', -1, 8) AS NEW_STRING;

Output

Following is the output of the above query −

+------------+
| NEW_STRING |
+------------+
| Tutori     |
+------------+

Example

If the start argument value is greater than the expression length.

In the following example, we are passing the start argument value as greater than the given expression. If the start argument value is greater than the number of characters in the value expression; else a zero-length expression is returned.

SELECT SUBSTRING('Java Programming', 30, 5) AS NEW_STRING;

Output

The above program generates the following output −

+------------+
| NEW_STRING |
+------------+
|            |
+------------+

Example

You can pass the table column name as an argument to the SUBSTRING() function to retrieve the part of characters from the column FIRST_NAME of the table. Assume we have created a table with the name Customers using the CREATE statement as follows −

CREATE TABLE CUSTOMERS(    
ID INT NOT NULL,    
FIRST_NAME VARCHAR (20),
LAST_NAME VARCHAR(20),s
AGE INT NOT NULL,    
ADDRESS CHAR (25) ,    
SALARY DECIMAL (18, 2));

Let’s insert some records into the Customers table using the INSERT statement as follows −

INSERT INTO CUSTOMERS VALUES (1, 'Rohan','Verma', 33, 'Hyderbad', 2100.00 ); 
INSERT INTO CUSTOMERS VALUES (2, 'Kamlesh','Kumar', 30, 'Lucknow', 2500.00 ); 
INSERT INTO CUSTOMERS VALUES (3, 'Seeta','Sharma', 23, 'Delhi', 3150.00 );

The following SQL query retrieves the part of the character(substring) from the content of the column FIRST_NAME of the Customer table −

SELECT ID, FIRST_NAME, SUBSTRING(FIRST_NAME, 2, 5) AS NEW_NAME FROM CUSTOMERS;

Output

Following is the output of the above SQL query −

+----+------------+----------+
| ID | FIRST_NAME | NEW_NAME |
+----+------------+----------+
|  1 | Rohan      | ohan     |
|  2 | Kamlesh    | amles    |
|  3 | Seeta      | eeta     |
+----+------------+----------+
sql-string-functions.htm
Advertisements