SQL Reverse String

How to write a SQL Query to reverse every word present in a string with an example?. SQL reverse string is one of the standard Interview Questions you might face in the interviews.

SQL Reverse String Word by Word Example

The SQL SUBSTRING Function allows you to extract and display the part of a string. In this SQL example, we show you how to Select the string before the space symbol and reverse those string words.

-- SQL Extract Domain From Email

DECLARE @Source VARCHAR(MAX)
DECLARE @Destination VARCHAR(MAX)
DECLARE @Length INT 

SET @Source = 'Welcome to SQL Server Tutorial Service at Tutorial Gateway'
SET @Destination = ''

WHILE LEN(@Source) > 0
  BEGIN
    IF CHARINDEX(' ', @Source) > 0
    BEGIN
	SET @Destination = SUBSTRING(@Source, 0, CHARINDEX(' ', @Source)) + ' ' + @Destination
	SET @Source = LTRIM(RTRIM(SUBSTRING(@Source, CHARINDEX(' ', @Source) + 1, LEN(@Source))))
    END
    ELSE
    BEGIN
	SET @Destination = @Source + ' ' + @Destination
	SET @Source = ''
    END
  END

SELECT @Destination
SQL Reverse String Words 1

First, we declared three variables using the DECLARE statement. Next, we used the WHILE Loop to iterate every character present in the @Source. For the condition inside the SQL Server while loop, we used the SQL LEN Function to check whether the length of the source is greater than 0 or not.

Within the while loop of SQL reverse string example, we used the SUBSTRING Function to set the @Destination value. And it allows three parameters, and they are Source, start point, endpoint. Here we assigned the

  • Source as @Source or you can use Column Name
  • Next, we used the starting position as 0.
  • Next, we used the CHARINDEX Function to find the ‘ ‘ So that the endpoint will be before the space.
  • Lastly, we are adding this to @Destination, and it is empty at the starting of the loop

To set the @Source value (it means to update the @source variable), we are using the SUBSTRING Function. Here we assigned the

  • Source as @Source or you can use Column Name
  • Next, we used the SQL CHARINDEX Function to find the empty space, and then we added 1. so that the starting point will be after space.
  • Lastly, we used the SQL LEN Function to specify the end value

From the above SQL reverse string analysis, you can observe that,

  • SET @Destination means we are extracting the word starting at 0 indexes and up to empty space. That will be the first word.
  • Once we got our first word, we will remove that word from @Source using the SET @Source code.

SQL Reverse String Word by Word Example 2

Although the above example will return the result, we want to wrap SQL reverse string in the User Defined Function. I suggest you refer to the User Defined Function article for better understanding.

-- SQL String Reverse Example - Reverse String words

USE [SQLTEST]
GO
CREATE FUNCTION [ReverseStringWords]
(
    @Source VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)  
BEGIN
         
   DECLARE @Destination VARCHAR(MAX)
   SET @Destination = '' 
    
   DECLARE @Length INT
     
   WHILE LEN(@Source) > 0
   BEGIN
      IF CHARINDEX(' ', @Source) > 0
      BEGIN
         SET @Destination = SUBSTRING(@Source, 0, CHARINDEX(' ', @Source)) + ' ' + @Destination
         SET @Source = LTRIM(RTRIM(SUBSTRING(@Source, CHARINDEX(' ', @Source) + 1, LEN(@Source))))
      END
      ELSE
      BEGIN
         SET @Destination = @Source + ' ' + @Destination
         SET @Source = ''
      END
   END          
   RETURN @Destination
END

We can find the user defined function in two ways. Please navigate to the Database that contains the function -> Programmability -> Scalar-valued Functions -> function name. Next, right click on the function name will open the context menu. Here, please choose the Script Function as -> Select To -> New Query Editor Window option.

SQL Reverse String Words 2

Or write the following code snippet to reverse string in SQL Server

-- SQL Server String Reverse Example
USE [SQLTEST]
GO

SELECT [dbo].[ReverseStringWords]('Welcome To SQL Server Tutorial Service at Tutorial Gateway')
GO
SQL Reverse String Words 3