- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
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 | +----+------------+----------+
To Continue Learning Please Login
Login with Google