User Defined Function (UDF) to Determine if a String is a Palindrome

Palindrome is a word or a phrase that reads the same in both directions. To check if a string is a palindrome in SQL Server, you can use REVERSE built in string function that returns the reverse order of a string value, and compare it to the original string.
We will create a User Defined Function (UDF) that accepts a string and checks if the string (with spaces removed from it) is a palindrome:

CREATE FUNCTION ufnPalindromeCheck (@MyString nvarchar(256))
RETURNS varchar(50)
AS
BEGIN
DECLARE @ReturnString varchar(50)
SET @ReturnString = CASE WHEN REVERSE (REPLACE(@MyString, ' ', '')) = (REPLACE(@MyString, ' ', ''))
THEN 'The string is a palindrome'
ELSE 'The string is NOT a palindrome'
END
RETURN @ReturnString
END
GO

To use this user-defined function:

SELECT dbo.ufnPalindromeCheck ('Step on no pets')

palindrome_check_udf

SELECT dbo.ufnPalindromeCheck ('So many dynamos')

palindrome_check_udf_2