How to use REVERSE in Sql Server

06.14.2022

Intro

SQL Server provides the REVERSE function to invert the order of the characters in a string. In this article, we will learn how to use REVERSE in SQL Server.

The Syntax

The basic syntax of a REVERSE is as follows:

SELECT REVERSE(string_name);
  • string is the string to reverse

Getting Setup

For this, we will be using docker. This is recommended for more than just using SQL Server. To find how to install docker go here: https://docs.docker.com/engine/install/

Now create a file called docker-compose.yml and add the following.

version: "3.9"
services:
  db:
    image: "mcr.microsoft.com/mssql/server"
    ports: 
      - 1433:1433
    environment:
        SA_PASSWORD: "Your_password123"
        ACCEPT_EULA: "Y"

Open a terminal and go to the folder the file is located. Then run the following.

docker-compose up

If you are looking for another good reference, you can check here: https://docs.docker.com/samples/aspnet-mssql-compose/.

A Simple Example

The reverse function is pretty straight forward, we simply reverse any varchar type. Here is the example.

SELECT reverse('hello world') as res;
res
dlrow olleh