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.
In
previous article I have explained how to create text file and download usingasp.net, how to add auto numbering/ serial number to rows in Angularjsng-repeat and how to set alternative color of column in column chart in asp.netchart control.
Description:
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
UNION all
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
No comments:
Post a Comment