Zahid Zahid - 4 months ago 17
SQL Question

How to check Palindrome in SQL Server

To check palindrome I am using

REVERSE
function of SQL Server.

I wanted to check how reverse function works with this sample code:

declare @string nvarchar
set @string = 'szaaa'
SELECT REVERSE(@string)


But the output was 's' in case of 'aaazs' which I expected. How should I capture the reverse? Is there any better way to find palindrome?

Answer

In SQL Server, always use lengths with the character types:

declare @string nvarchar(255);
set @string = 'szaaa';

SELECT REVERSE(@string);

The default length varies by context. In this case, the default length is "1", so the string variable only holds one character.