I have a SQL code for finding whether two words are anagram or not.
DECLARE @str1 VARCHAR(100), @str2 VARCHAR(100)
SELECT @str1 = 'mmaa', @str2 = 'mama'
IF LEN(@str1) <> LEN(@str2)
BEGIN
SELECT 'NOT EQUAL'
END
ELSE
BEGIN
IF (SELECT COUNT(*) FROM
(
select substring(@str1, number, 1) as data
from master..spt_values as m
where m.type='p' and number <= len(@str1) AND number > 0
UNION
select substring(@str2, number, 1) as data
from master..spt_values as m
where m.type='p' and number <= len(@str2) AND number > 0
)
t)
= LEN(@str1)
SELECT '1 anagram'
ELSE
SELECT '0 not anagram'
END
But for words like mmaa and mama this program returns not anagram. But they are anagrams. How can I solve this problem?
rather crude, but how about:
or to encapsulate the sort into a function,