Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9121191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:48:16+00:00 2026-06-17T05:48:16+00:00

I have a SQL code for finding whether two words are anagram or not.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:48:17+00:00Added an answer on June 17, 2026 at 5:48 am

    rather crude, but how about:

    Declare @S1 varChar(100) = 'mmaa'
    Declare @S2 varChar(100) = 'mama'
    
    Declare @c char(1)
    Declare @i tinyint
    Declare @o1 varChar(100) = ''
    Declare @o2 varChar(100) = ''
    While DataLength(@s1) > 0 Begin
       Set @c = Left(@s1, 1)
       Set @s1 = Substring(@s1, 2, len(@s1))
       Set @i = 1
    
       While @i <= Len(@o1) And @c > substring(@o1, @i, 1) Set @i += 1
       Set @o1 = left(@o1, @i-1) + @c + substring(@o1, @i, len(@o1))
    End
    While DataLength(@s2) > 0 Begin
       Set @c = Left(@s2, 1)
       Set @s2 = Substring(@s2, 2, len(@s2))
       Set @i = 1
    
       While @i <= Len(@o2) And @c > substring(@o2, @i, 1) Set @i += 1
       Set @o2 = left(@o2, @i-1) + @c + substring(@o2, @i, len(@o2))
    End
    Select case When @o1 = @o2 Then 'Anagram' Else 'Not Anagram' End
    

    or to encapsulate the sort into a function,

        Create FUNCTION [dbo].[SortString](@s varChar(8000))
        RETURNS varChar(8000)
        As
        Begin
            Declare @c char(1)
            Declare @i int
            Declare @out varChar(8000) = left(@s, 1)
            While DataLength(@s) > 0 Begin
                Set @s = Substring(@s, 2, len(@s)-1)
                Set @c = Left(@s, 1)
                Set @i = 0
                While @i < Len(@out) And @c > substring(@out, @i+1, 1) Set @i += 1
                Set @out = case when @i < Len(@out) 
            then stuff(@out, @i+1, 0, @c)
            else  @out + @c end  
            End
            return  @out
        End
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this SQL code here.. SELECT cl.clientid, cl.clientname, cl.billingdate, cp.startdate, cp.expiration, (SELECT COUNT(*)
I have following tsql code in sql server 2008: declare @ID INT SET @ID
I have the following SQL code that runs against a Change Request database. Each
i have a sql server that i use and i am trying to code
I have a SQL Server table with 2 columns, Code and CodeDesc. I want
Following Uncle Bob's avice in Clean Code , I'd like to have no SQL
In my Java code I have embedded a SQL query which fetches data from
I have code to run sql query in ruby as follows sql = ActiveRecord::Base.connection()
I have this code to update my SQL database from data in a textbox,
I have some code that uses the SQL Server 2005 SMO objects to backup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.