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 6990303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:19:49+00:00 2026-05-27T19:19:49+00:00

I need a function in sql server to build all of changed word in

  • 0

I need a function in sql server to build all of changed word in below example;
for input word with length n must build 2^n changed word;
For example, if the input of the function is

"I"

the output of the function should be

I
-   

the input of the function is

"am"

the output of the function should be

am
-m
a-
--

the input of the function is

"sql"

the output of the function should be

sql
-ql
s-l
sq-
--l
s--
-q-
--- 
  • 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-05-27T19:19:50+00:00Added an answer on May 27, 2026 at 7:19 pm

    You can do this with a numbers table (master..spt_values) and stuff in a loop.

    declare @Word varchar(10) = 'sql'
    
    declare @T table
    (
      Word varchar(10)
    )
    
    insert into @T values (@Word)
    
    while not exists(select *
                     from @T 
                     where Word = replicate('-', len(@Word)))
    begin              
      insert into @T(Word)
      select distinct stuff(T.Word, N.number, 1, '-')
      from @T as T
        cross join
           master..spt_values as N
      where N.type = 'P' and
            N.number between 1 and len(@Word) and
            stuff(T.Word, N.number, 1, '-') not in (select Word from @T)
    end        
    
    select *
    from @T
    

    https://data.stackexchange.com/stackoverflow/q/122334/

    Or you can use a reqursive CTE

    declare @Word varchar(10) = 'sql'
    
    ;with C as
    (
      select @Word as Word,
             0 as Iteration
      union all
      select cast(stuff(Word, N.number, 1, '-') as varchar(10)),
             Iteration + 1
      from C
        cross join
           master..spt_values as N
      where N.type = 'P' and
            N.number between 1 and len(@Word) and
            Iteration < len(@Word)
    )
    select distinct Word
    from C
    

    https://data.stackexchange.com/stackoverflow/q/122337/

    Update

    The recursive CTE version is really slow as pointed out by OP in a comment. Using a word with 7 letters there are 960800 rows returned from the CTE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function for all distinct values in my table in SQL Server.
I am working with SQL Server 2005. I need to select all rows from
I need similar function of strtotime( next monday ) in SQL Server.
I need a function in sql server to insert a space in each position
I need to create a function in SQL Server that returns a string. In
I need to have a sql function which takes a valid SQL Server date
I need a function that I can call when somthing is done, for example
I need a function that can return the difference between the below two dates
I need to convert a SQL Server DATETIME value to FILETIME in a T-SQL
I'm changing the security for a SQL Server 2005 database, so I need to

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.