Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Palindrome in SQL Server

Tags:

sql

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?

like image 628
BlackCat Avatar asked Sep 11 '25 20:09

BlackCat


1 Answers

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.

like image 178
Gordon Linoff Avatar answered Sep 14 '25 13:09

Gordon Linoff