How to use SUBSTRING on a Nested Queries In SQL
Assuming you want to use the SUBSTRING function on a nested query within the player_Details table, you could use the following SQL code
Query
SELECT SUBSTRING(subquery.PlayerName, 1, 3) AS ShortenedName, subquery.City
FROM (
SELECT *
FROM Player_Details
WHERE City = 'New York'
) AS subquery;
Output
In order to select all rows from the Player_Details table where the City column equals “New York,” this SQL query first creates a nested query. A derived table or subquery that contains the results of this nested query is then used as the input for the SUBSTRING function.
The PlayerName column in the subquery receives the SUBSTRING function, which chooses the first three characters of each name. The outcome of this substring operation is then returned alongside the City column from the subquery in a new column with the alias ShortenedName.
SQL Server SUBSTRING() Function
SUBSTRING function in SQL Server is used to extract a substring from a string, starting at a specified position and with an optional length.
It is very useful when you need to extract a specific portion of a string for further processing or analysis.
SQL SUBSTRING function also works in Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse.
Contact Us