SQL Server REVERSE()

In this SQL Server tutorial, you will learn about the SQL Server REVERSE() function to change the order of the string value.

This function is very helpful in the data manipulation of strings, such as creating unique codes, data formatting, etc.

You will understand the syntax of the REVERSE() function with its definition, and then you will reverse the literal string, integer value, and float value. Afterwards, you will learn an example of how to reverse the value of a variable.

Finally, you will understand how to use the REVERSE() function on the table column to reverse the column’s values.

SQL Server REVERSE()

SQL Server REVERSE() function reveres the characters in the specified string. It changes the order of the characters in a string value. For example, it reverses the word from ‘ABC’ to ‘CBA’.

  • The REVERSE() function flips the characters in the specified string.

The syntax is given below.

REVERSE(string_value)

Where,

  • REVERSE(): This is the function which reverses the string.
  • string_value: The string value whose characters you want to flip or reverse. This can be a column, variable or literal value. Also, the string value must be convertible to varchar implicitly. Otherwise, change the value’s data type into varchar using the CAST() function.

The return type of the REVERSE() function is varchar or nvarchar.

Let’s take an example and reverse the string value ‘Chicago’, using the query below.

SELECT REVERSE('Chicago') AS ReversedString;
SQL Server REVERSE Literal String

As you can see in the above result set, the string ‘Chicago’ is reversed as ‘ogacihC’, so here, the first letter of the string becomes the last letter in the reversed string and the same for all the other letters or characters, in the string.

This is how the SQL Server REVERSE() function works.

Using Integer with SQL Server REVERSE()

If you have a set of integer values and want to reverse the integer values, you can use the REVERSE() function for that.

For example, the set of integer values is 4356, so to reverse this number, use the query below.

SELECT REVERSE(4356) AS ReverseIntegerValue;
Using Integer with SQL Server REVERSE

The set of integer values 4356 is reversed to 6534, as you can see in the output of the above query.

But here, we haven’t specified the string value; instead, we provided the integer values, so here REVERSE() function implicitly converts the integer value to varchar and reverses the character in the converted string.

This is how the SQL Server REVERSE() function converts the non-string value into a string value and then reverses the order of the characters in that string.

Using Float with SQL Server REVERSE()

In the previous section, reverse the set of integer values using the SQL Server REVERSE() function. Here, you will see how to reverse the float value in reverse order.

For example, the area of the United States is 9.834 million Km2, as you can see, the area is a float value, so to reverse this float value, use the query below.

SELECT REVERSE(9.834) AS ReverseFloatValue;
Using Float with SQL Server REVERSE()

The float value 9.384 is reversed to 438.9, as shown in the above output. Again, here, first, the float value is converted into varchar implicitly and then the characters are reversed.

This is how to reverse the float value using the SQL Server REVERSE() function.

SQL Server REVERSE() Value in a Variable

You can also reverse the value in a variable. When defining the stored procedure or function, you often use the variable while operating. If you need to reverse the value of a variable, then you can use the REVERSE() function.

For example, let’s you have variable @country containing the value ‘USA’, and reverse the string value in the variable, use the query below.

DECLARE @country VARCHAR(10) = 'USA';
SELECT REVERSE(@country) AS ReverseStringValue;
SQL Server REVERSE() Value in a Variable

The above output shows that the value ‘USA’ in the @country variable is reversed to ‘ASU’.

This is how to reverse the value in a variable using the SQL Server REVERSE() function.

Using SQL Server REVERSE() Table Column

Here, you will see how you can use the REVERSE() function with the table column or reverse the order of the values in the table column.

For example, you have a Sales table, as shown below.

Using SQL Server REVERSE() Sales Table Column

For example, suppose you have to reverse the name of all the products in the ProductName column of the Sales table. For that, look at the query below.

SELECT ProductName, REVERSE(ProductName) AS ReversedProductName
FROM Sales;
Using SQL Server REVERSE() Table Column

All the values in the ProductName column are reversed in the above query output in the ReversedProductName column indicated by the brown down arrow.

Also, you can apply the REVERSE() function on the SalesPrice column, which contains the float value. For example, use the query below to reverse all the product sale prices in the sales price column.

SELECT SalesPrice, REVERSE(SalesPrice) AS ReversedSalesPrice
FROM Sales;
Using SQL Server REVERSE() Table Column Containing Float Values

All the sale price values in the SalesPrice column are reversed, as shown in the above output.

This is how to use the SQL Server REVERSE() function on the table column to reverse the column’s values.

Conclusion

In this SQL Server tutorial, you learned how to use the SQL Server REVERSE() function to reverse the character in the specified string value. Where you have reversed the string literal value, integer value, and float value.

Also, you learned how to reverse the value stored in a variable and then the table column values.

You may like to read: