Sql Server : Check string is Palindrome or not

In this article I am going to explain how to check string is Palindrome or not using Reverse function or without reverse function in sql server.

I want to check given text/string is Palindrome or not. Simplest way to check is using reverse function.
* Palindrome means word that reads same forward as backward.

 Check Palindrome using Reverse function:

declare @palindromestring varchar(200) ='Level'
select @palindromestring as [Text],REVERSE(@palindromestring) as [Reverse Text],case when
@palindromestring=REVERSE(@palindromestring) then 'Yes' else 'No' END AS Palindrome

Check Palindrome without reverse function:

DECLARE @palindromestring VARCHAR(100) ='Level'
;with revcte AS
SELECT CAST('' AS VARCHAR(100)) AS reversestr,LEN(@palindromestring) AS ln
SELECT CAST(reversestr+SUBSTRING(@palindromestring,ln,1) AS VARCHAR(100)) AS reversestr,
ln-1 AS ln FROM revcte WHERE ln>=1
SELECT @palindromestring as [Text], reversestr as [Reverse Text] FROM revcte WHERE ln=0

Post a Comment



Contact Form


Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget