Zahid Zahid - 1 year ago 75
SQL Question

How to check Palindrome in SQL Server

To check palindrome I am using

function of SQL Server.

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

declare @string nvarchar
set @string = 'szaaa'

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 Source

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

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


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