Permutation and Combination of wordsGenerate permutations and combination for name columnWhat Happens When Identity Range IsExceeded?Oracle GoldenGate add trandata errorsSelect Into removes IDENTITY property from target tableSpeeding up insert in SQL ServerWhy am I getting “Snapshot isolation transaction aborted due to update conflict”?Investigating errors from strange queryProblems with insert trigger and primary keyInsert results of spBlitzIndex stored procedure into tableGroup by sum based on under group in SQL ServerTrigger to decrypt the Encrypted data in a column while it's being inserted

Who wrote “A writer only begins a book. A reader finishes it.”?

Why is the Eisenstein ideal paper so great?

Why does the painters tape have to be blue?

Can I render satellite deployment impossible, or at least impractical, by exploiting the Kessler syndrome?

Why did Drogon spare this character?

Did significant numbers of Japanese officers escape prosecution during the Tokyo Trials?

What is to the west of Westeros?

"Official wife" or "Formal wife"?

Alexandrov's generalization of Cauchy's rigidity theorem

Reduce size of sum sub/superscript?

Why'd a rational buyer offer to buy with no conditions precedent?

Was this scene in S8E06 added because of fan reactions to S8E04?

Possibility of faking someone's public key

Papers on ArXiv as main references

One word for 'the thing that attracts me'?

Writing "hahaha" versus describing the laugh

Where is Jon going?

Moons and messages

Why isn't Tyrion mentioned in 'A song of Ice and Fire'?

Fill area of x^2+y^2>1 and x^2+y^2>4 using patterns and tikzpicture

To exponential digit growth and beyond!

Count all vowels in string

Toxic, harassing lab environment

How to write numbers and percentage?



Permutation and Combination of words


Generate permutations and combination for name columnWhat Happens When Identity Range IsExceeded?Oracle GoldenGate add trandata errorsSelect Into removes IDENTITY property from target tableSpeeding up insert in SQL ServerWhy am I getting “Snapshot isolation transaction aborted due to update conflict”?Investigating errors from strange queryProblems with insert trigger and primary keyInsert results of spBlitzIndex stored procedure into tableGroup by sum based on under group in SQL ServerTrigger to decrypt the Encrypted data in a column while it's being inserted






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4















Follow up of question



I have the following data:



I have the following sample data for permutations and combination.



create table tbltest
(
name varchar(50),
addres varchar(100)
);

insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
insert into tbltest values('Donatella Nobatti','HNo 101 UK');
insert into tbltest values('Sam Buca','Main Road B Block UAE');
insert into tbltest values('Juan Soponatime','Hight Street CA');
insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


For an example expected result:



name
----------------
John Mak Sam
John Sam Mak
Mak John Sam
Mak Sam John
Sam John Mak
Sam Mak John
....
....


Tried the solution from previous question:



--To store Words 
CREATE TABLE dbo.NameParts
(
ID int NOT NULL,
NamePart varchar(30) NOT NULL
);

--Table variable to identity column for each words
DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

--Insert into table variable
INSERT INTO @Inter(names) SELECT name from TblTest;

--Query to insert into NameParts table
;WITH splitdata
AS
(
SELECT id,splitname
FROM
(
SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
FROM @Inter F
)F1
CROSS apply
(
SELECT fdata.d.value('.', 'varchar(50)') AS splitName
FROM f1.xmlfilter.nodes('X') AS fdata(d)
) O
)
INSERT INTO dbo.NameParts
SELECT *
FROM splitdata;


Query for Permutation and Combination:



--Query for Permutation and Combination
;WITH
permutations AS
(
SELECT
ID = t.ID,
FullNameVariation = CAST(t.NamePart AS varchar(500)),
Level = COUNT(*) OVER (PARTITION BY t.ID)
FROM
dbo.NameParts AS t
UNION ALL
SELECT
ID = t.ID,
FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
Level = p.Level - 1
FROM
dbo.NameParts AS t
INNER JOIN permutations AS p ON t.ID = p.ID
WHERE 1=1
AND p.Level > 1
AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
)
SELECT
ID,
FullNameVariation
FROM
permutations
WHERE
Level = 1
ORDER BY
ID,
FullNameVariation
OPTION (MAXRECURSION 5100);


Note: Above query works fine but unable to get following record in the result set:



Nasir Ahmed Amir Ahmed Mehendy



and that is because of this condition:



' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'


The word Ahmed is repeated and the above condition not met.










share|improve this question






















  • Check my script, Can you give example of 'Nasir Ahmed Amir Ahmed Mehendy' What is Combination of this ?

    – KumarHarsh
    May 15 at 11:06











  • check my answer. Can you give output of repeated words like Nasir Ahmed Amir Ahmed Mehendy

    – KumarHarsh
    May 17 at 6:26

















4















Follow up of question



I have the following data:



I have the following sample data for permutations and combination.



create table tbltest
(
name varchar(50),
addres varchar(100)
);

insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
insert into tbltest values('Donatella Nobatti','HNo 101 UK');
insert into tbltest values('Sam Buca','Main Road B Block UAE');
insert into tbltest values('Juan Soponatime','Hight Street CA');
insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


For an example expected result:



name
----------------
John Mak Sam
John Sam Mak
Mak John Sam
Mak Sam John
Sam John Mak
Sam Mak John
....
....


Tried the solution from previous question:



--To store Words 
CREATE TABLE dbo.NameParts
(
ID int NOT NULL,
NamePart varchar(30) NOT NULL
);

--Table variable to identity column for each words
DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

--Insert into table variable
INSERT INTO @Inter(names) SELECT name from TblTest;

--Query to insert into NameParts table
;WITH splitdata
AS
(
SELECT id,splitname
FROM
(
SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
FROM @Inter F
)F1
CROSS apply
(
SELECT fdata.d.value('.', 'varchar(50)') AS splitName
FROM f1.xmlfilter.nodes('X') AS fdata(d)
) O
)
INSERT INTO dbo.NameParts
SELECT *
FROM splitdata;


Query for Permutation and Combination:



--Query for Permutation and Combination
;WITH
permutations AS
(
SELECT
ID = t.ID,
FullNameVariation = CAST(t.NamePart AS varchar(500)),
Level = COUNT(*) OVER (PARTITION BY t.ID)
FROM
dbo.NameParts AS t
UNION ALL
SELECT
ID = t.ID,
FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
Level = p.Level - 1
FROM
dbo.NameParts AS t
INNER JOIN permutations AS p ON t.ID = p.ID
WHERE 1=1
AND p.Level > 1
AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
)
SELECT
ID,
FullNameVariation
FROM
permutations
WHERE
Level = 1
ORDER BY
ID,
FullNameVariation
OPTION (MAXRECURSION 5100);


Note: Above query works fine but unable to get following record in the result set:



Nasir Ahmed Amir Ahmed Mehendy



and that is because of this condition:



' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'


The word Ahmed is repeated and the above condition not met.










share|improve this question






















  • Check my script, Can you give example of 'Nasir Ahmed Amir Ahmed Mehendy' What is Combination of this ?

    – KumarHarsh
    May 15 at 11:06











  • check my answer. Can you give output of repeated words like Nasir Ahmed Amir Ahmed Mehendy

    – KumarHarsh
    May 17 at 6:26













4












4








4


1






Follow up of question



I have the following data:



I have the following sample data for permutations and combination.



create table tbltest
(
name varchar(50),
addres varchar(100)
);

insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
insert into tbltest values('Donatella Nobatti','HNo 101 UK');
insert into tbltest values('Sam Buca','Main Road B Block UAE');
insert into tbltest values('Juan Soponatime','Hight Street CA');
insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


For an example expected result:



name
----------------
John Mak Sam
John Sam Mak
Mak John Sam
Mak Sam John
Sam John Mak
Sam Mak John
....
....


Tried the solution from previous question:



--To store Words 
CREATE TABLE dbo.NameParts
(
ID int NOT NULL,
NamePart varchar(30) NOT NULL
);

--Table variable to identity column for each words
DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

--Insert into table variable
INSERT INTO @Inter(names) SELECT name from TblTest;

--Query to insert into NameParts table
;WITH splitdata
AS
(
SELECT id,splitname
FROM
(
SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
FROM @Inter F
)F1
CROSS apply
(
SELECT fdata.d.value('.', 'varchar(50)') AS splitName
FROM f1.xmlfilter.nodes('X') AS fdata(d)
) O
)
INSERT INTO dbo.NameParts
SELECT *
FROM splitdata;


Query for Permutation and Combination:



--Query for Permutation and Combination
;WITH
permutations AS
(
SELECT
ID = t.ID,
FullNameVariation = CAST(t.NamePart AS varchar(500)),
Level = COUNT(*) OVER (PARTITION BY t.ID)
FROM
dbo.NameParts AS t
UNION ALL
SELECT
ID = t.ID,
FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
Level = p.Level - 1
FROM
dbo.NameParts AS t
INNER JOIN permutations AS p ON t.ID = p.ID
WHERE 1=1
AND p.Level > 1
AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
)
SELECT
ID,
FullNameVariation
FROM
permutations
WHERE
Level = 1
ORDER BY
ID,
FullNameVariation
OPTION (MAXRECURSION 5100);


Note: Above query works fine but unable to get following record in the result set:



Nasir Ahmed Amir Ahmed Mehendy



and that is because of this condition:



' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'


The word Ahmed is repeated and the above condition not met.










share|improve this question














Follow up of question



I have the following data:



I have the following sample data for permutations and combination.



create table tbltest
(
name varchar(50),
addres varchar(100)
);

insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
insert into tbltest values('Donatella Nobatti','HNo 101 UK');
insert into tbltest values('Sam Buca','Main Road B Block UAE');
insert into tbltest values('Juan Soponatime','Hight Street CA');
insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


For an example expected result:



name
----------------
John Mak Sam
John Sam Mak
Mak John Sam
Mak Sam John
Sam John Mak
Sam Mak John
....
....


Tried the solution from previous question:



--To store Words 
CREATE TABLE dbo.NameParts
(
ID int NOT NULL,
NamePart varchar(30) NOT NULL
);

--Table variable to identity column for each words
DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

--Insert into table variable
INSERT INTO @Inter(names) SELECT name from TblTest;

--Query to insert into NameParts table
;WITH splitdata
AS
(
SELECT id,splitname
FROM
(
SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
FROM @Inter F
)F1
CROSS apply
(
SELECT fdata.d.value('.', 'varchar(50)') AS splitName
FROM f1.xmlfilter.nodes('X') AS fdata(d)
) O
)
INSERT INTO dbo.NameParts
SELECT *
FROM splitdata;


Query for Permutation and Combination:



--Query for Permutation and Combination
;WITH
permutations AS
(
SELECT
ID = t.ID,
FullNameVariation = CAST(t.NamePart AS varchar(500)),
Level = COUNT(*) OVER (PARTITION BY t.ID)
FROM
dbo.NameParts AS t
UNION ALL
SELECT
ID = t.ID,
FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
Level = p.Level - 1
FROM
dbo.NameParts AS t
INNER JOIN permutations AS p ON t.ID = p.ID
WHERE 1=1
AND p.Level > 1
AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
)
SELECT
ID,
FullNameVariation
FROM
permutations
WHERE
Level = 1
ORDER BY
ID,
FullNameVariation
OPTION (MAXRECURSION 5100);


Note: Above query works fine but unable to get following record in the result set:



Nasir Ahmed Amir Ahmed Mehendy



and that is because of this condition:



' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'


The word Ahmed is repeated and the above condition not met.







sql-server sql-server-2008-r2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked May 9 at 7:55









MAKMAK

1785




1785












  • Check my script, Can you give example of 'Nasir Ahmed Amir Ahmed Mehendy' What is Combination of this ?

    – KumarHarsh
    May 15 at 11:06











  • check my answer. Can you give output of repeated words like Nasir Ahmed Amir Ahmed Mehendy

    – KumarHarsh
    May 17 at 6:26

















  • Check my script, Can you give example of 'Nasir Ahmed Amir Ahmed Mehendy' What is Combination of this ?

    – KumarHarsh
    May 15 at 11:06











  • check my answer. Can you give output of repeated words like Nasir Ahmed Amir Ahmed Mehendy

    – KumarHarsh
    May 17 at 6:26
















Check my script, Can you give example of 'Nasir Ahmed Amir Ahmed Mehendy' What is Combination of this ?

– KumarHarsh
May 15 at 11:06





Check my script, Can you give example of 'Nasir Ahmed Amir Ahmed Mehendy' What is Combination of this ?

– KumarHarsh
May 15 at 11:06













check my answer. Can you give output of repeated words like Nasir Ahmed Amir Ahmed Mehendy

– KumarHarsh
May 17 at 6:26





check my answer. Can you give output of repeated words like Nasir Ahmed Amir Ahmed Mehendy

– KumarHarsh
May 17 at 6:26










2 Answers
2






active

oldest

votes


















9














Extending the original answer by Andriy M we can uniqify the strings at the insert phase with a dummy character (in my case I used ~), and remove that character later.



In this case I chose to use REPLICATE passing in the dummy character '~' and the ROW_NUMBER function for the number of instances to replicate. This should ensure that all strings are unique.



dbFiddle



Full code



create table tbltest
(
name varchar(50),
addres varchar(100)
);

insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
insert into tbltest values('Donatella Nobatti','HNo 101 UK');
insert into tbltest values('Sam Buca','Main Road B Block UAE');
insert into tbltest values('Juan Soponatime','Hight Street CA');
insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');
GO

CREATE TABLE dbo.NameParts
(
ID int NOT NULL,
NamePart varchar(30) NOT NULL
);

--Table variable to identity column for each words
DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

--Insert into table variable
INSERT INTO @Inter(names) SELECT name from TblTest;

--Query to insert into NameParts table
;WITH splitdata
AS
(
SELECT id,splitname
FROM
(
SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
FROM @Inter F
)F1
CROSS apply
(
SELECT fdata.d.value('.', 'varchar(50)') AS splitName
FROM f1.xmlfilter.nodes('X') AS fdata(d)
) O
)
INSERT INTO dbo.NameParts
SELECT id,[splitdata].[splitName] + REPLICATE('~',(ROW_NUMBER() OVER (PARTITION BY [splitdata].[id] ORDER BY [splitdata].[splitName] ASC))
FROM splitdata;
GO

;WITH
permutations AS
(
SELECT
ID = t.ID,
FullNameVariation = CAST(t.NamePart AS varchar(500)),
Level = COUNT(*) OVER (PARTITION BY t.ID)
FROM
dbo.NameParts AS t
UNION ALL
SELECT
ID = t.ID,
FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
Level = p.Level - 1
FROM
dbo.NameParts AS t
INNER JOIN permutations AS p ON t.ID = p.ID
WHERE 1=1
AND p.Level > 1
AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
)
SELECT
ID,
REPLACE(FullNameVariation,'~','')
FROM
permutations
WHERE 1=1 AND
Level = 1
ORDER BY
ID,
FullNameVariation
OPTION (MAXRECURSION 5100);

DROP TABLE [dbo].[tbltest]
DROP TABLE [dbo].[NameParts]





share|improve this answer
































    0














    You didn't mention number of rows return in output if Ahmed is repeated.



    In my script, 74 rows are return. It count Ahmed as one.



    If different Output is desire then show the Example of repeated word like 'Ahmed'



    Any Split string function that also return row number.



    CREATE FUNCTION [dbo].[DelimitedSplit2K]

    (@pString VARCHAR(2000), @pDelimiter CHAR(1))

    RETURNS TABLE WITH SCHEMABINDING AS
    RETURN

    WITH E1(N) AS (
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
    ), --10E+1 or 10 rows
    E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
    E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
    cteTally(N) AS (

    SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
    ),
    cteStart(N1) AS (
    SELECT 1 UNION ALL
    SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
    ),
    cteLen(N1,L1) AS(
    SELECT s.N1,
    ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
    FROM cteStart s
    )

    SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
    Item = SUBSTRING(@pString, l.N1, l.L1)
    FROM cteLen l
    ;


    Number Table,



    create table tblnumber(number int not null)
    insert into tblnumber (number)
    select ROW_NUMBER()over(order by a.number) from master..spt_values a
    , master..spt_values b
    CREATE unique clustered index CI_num on tblnumber(number)


    Sample Data,



    create table #tbltest
    (
    id int identity(1,1),
    name varchar(50),
    addres varchar(100)
    );

    insert into #tbltest values('Sam Mak John','Street 1 HNo 101 USA');
    insert into #tbltest values('Donatella Nobatti','HNo 101 UK');
    insert into #tbltest values('Sam Buca','Main Road B Block UAE');
    insert into #tbltest values('Juan Soponatime','Hight Street CA');
    insert into #tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
    insert into #tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


    The Scripts (WITHOUT RBAR),



    create table #tbltest1
    (
    id int identity(1,1),
    name varchar(50)
    ,GroupID int
    ,rownum int

    );
    insert into #tbltest1 (name,GroupID,rownum)
    select item,t.id, ca.ItemNumber from #tbltest t
    cross apply(select * from dbo.DelimitedSplit2K(t.name,' '))ca


    DECLARE
    @max integer =
    POWER(
    (
    SELECT COUNT(*)
    FROM #tbltest1 AS s
    )
    ,3
    ) ;


    ;With CTE as
    (
    SELECT s.id
    ,s.GroupID
    ,ltrim((select ' '+name from #tbltest1 t1 where t1.GroupID =s.groupid order by (CRYPT_GEN_RANDOM(c.n)) for xml PATH('') ))name
    FROM #tbltest1 AS s
    CROSS APPLY (
    select top (@max) number n FROM dbo.tblnumber
    )c

    )

    select distinct name from CTE
    --where groupid=1


    drop table #tbltest
    drop table #tbltest1


    How many rows will be process at one time ?



    How many rows are there is table ?



    Above all how many average word can be there in column Name






    share|improve this answer























      Your Answer








      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "182"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f237720%2fpermutation-and-combination-of-words%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      9














      Extending the original answer by Andriy M we can uniqify the strings at the insert phase with a dummy character (in my case I used ~), and remove that character later.



      In this case I chose to use REPLICATE passing in the dummy character '~' and the ROW_NUMBER function for the number of instances to replicate. This should ensure that all strings are unique.



      dbFiddle



      Full code



      create table tbltest
      (
      name varchar(50),
      addres varchar(100)
      );

      insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
      insert into tbltest values('Donatella Nobatti','HNo 101 UK');
      insert into tbltest values('Sam Buca','Main Road B Block UAE');
      insert into tbltest values('Juan Soponatime','Hight Street CA');
      insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
      insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');
      GO

      CREATE TABLE dbo.NameParts
      (
      ID int NOT NULL,
      NamePart varchar(30) NOT NULL
      );

      --Table variable to identity column for each words
      DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

      --Insert into table variable
      INSERT INTO @Inter(names) SELECT name from TblTest;

      --Query to insert into NameParts table
      ;WITH splitdata
      AS
      (
      SELECT id,splitname
      FROM
      (
      SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
      FROM @Inter F
      )F1
      CROSS apply
      (
      SELECT fdata.d.value('.', 'varchar(50)') AS splitName
      FROM f1.xmlfilter.nodes('X') AS fdata(d)
      ) O
      )
      INSERT INTO dbo.NameParts
      SELECT id,[splitdata].[splitName] + REPLICATE('~',(ROW_NUMBER() OVER (PARTITION BY [splitdata].[id] ORDER BY [splitdata].[splitName] ASC))
      FROM splitdata;
      GO

      ;WITH
      permutations AS
      (
      SELECT
      ID = t.ID,
      FullNameVariation = CAST(t.NamePart AS varchar(500)),
      Level = COUNT(*) OVER (PARTITION BY t.ID)
      FROM
      dbo.NameParts AS t
      UNION ALL
      SELECT
      ID = t.ID,
      FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
      Level = p.Level - 1
      FROM
      dbo.NameParts AS t
      INNER JOIN permutations AS p ON t.ID = p.ID
      WHERE 1=1
      AND p.Level > 1
      AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
      )
      SELECT
      ID,
      REPLACE(FullNameVariation,'~','')
      FROM
      permutations
      WHERE 1=1 AND
      Level = 1
      ORDER BY
      ID,
      FullNameVariation
      OPTION (MAXRECURSION 5100);

      DROP TABLE [dbo].[tbltest]
      DROP TABLE [dbo].[NameParts]





      share|improve this answer





























        9














        Extending the original answer by Andriy M we can uniqify the strings at the insert phase with a dummy character (in my case I used ~), and remove that character later.



        In this case I chose to use REPLICATE passing in the dummy character '~' and the ROW_NUMBER function for the number of instances to replicate. This should ensure that all strings are unique.



        dbFiddle



        Full code



        create table tbltest
        (
        name varchar(50),
        addres varchar(100)
        );

        insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
        insert into tbltest values('Donatella Nobatti','HNo 101 UK');
        insert into tbltest values('Sam Buca','Main Road B Block UAE');
        insert into tbltest values('Juan Soponatime','Hight Street CA');
        insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
        insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');
        GO

        CREATE TABLE dbo.NameParts
        (
        ID int NOT NULL,
        NamePart varchar(30) NOT NULL
        );

        --Table variable to identity column for each words
        DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

        --Insert into table variable
        INSERT INTO @Inter(names) SELECT name from TblTest;

        --Query to insert into NameParts table
        ;WITH splitdata
        AS
        (
        SELECT id,splitname
        FROM
        (
        SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
        FROM @Inter F
        )F1
        CROSS apply
        (
        SELECT fdata.d.value('.', 'varchar(50)') AS splitName
        FROM f1.xmlfilter.nodes('X') AS fdata(d)
        ) O
        )
        INSERT INTO dbo.NameParts
        SELECT id,[splitdata].[splitName] + REPLICATE('~',(ROW_NUMBER() OVER (PARTITION BY [splitdata].[id] ORDER BY [splitdata].[splitName] ASC))
        FROM splitdata;
        GO

        ;WITH
        permutations AS
        (
        SELECT
        ID = t.ID,
        FullNameVariation = CAST(t.NamePart AS varchar(500)),
        Level = COUNT(*) OVER (PARTITION BY t.ID)
        FROM
        dbo.NameParts AS t
        UNION ALL
        SELECT
        ID = t.ID,
        FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
        Level = p.Level - 1
        FROM
        dbo.NameParts AS t
        INNER JOIN permutations AS p ON t.ID = p.ID
        WHERE 1=1
        AND p.Level > 1
        AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
        )
        SELECT
        ID,
        REPLACE(FullNameVariation,'~','')
        FROM
        permutations
        WHERE 1=1 AND
        Level = 1
        ORDER BY
        ID,
        FullNameVariation
        OPTION (MAXRECURSION 5100);

        DROP TABLE [dbo].[tbltest]
        DROP TABLE [dbo].[NameParts]





        share|improve this answer



























          9












          9








          9







          Extending the original answer by Andriy M we can uniqify the strings at the insert phase with a dummy character (in my case I used ~), and remove that character later.



          In this case I chose to use REPLICATE passing in the dummy character '~' and the ROW_NUMBER function for the number of instances to replicate. This should ensure that all strings are unique.



          dbFiddle



          Full code



          create table tbltest
          (
          name varchar(50),
          addres varchar(100)
          );

          insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
          insert into tbltest values('Donatella Nobatti','HNo 101 UK');
          insert into tbltest values('Sam Buca','Main Road B Block UAE');
          insert into tbltest values('Juan Soponatime','Hight Street CA');
          insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
          insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');
          GO

          CREATE TABLE dbo.NameParts
          (
          ID int NOT NULL,
          NamePart varchar(30) NOT NULL
          );

          --Table variable to identity column for each words
          DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

          --Insert into table variable
          INSERT INTO @Inter(names) SELECT name from TblTest;

          --Query to insert into NameParts table
          ;WITH splitdata
          AS
          (
          SELECT id,splitname
          FROM
          (
          SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
          FROM @Inter F
          )F1
          CROSS apply
          (
          SELECT fdata.d.value('.', 'varchar(50)') AS splitName
          FROM f1.xmlfilter.nodes('X') AS fdata(d)
          ) O
          )
          INSERT INTO dbo.NameParts
          SELECT id,[splitdata].[splitName] + REPLICATE('~',(ROW_NUMBER() OVER (PARTITION BY [splitdata].[id] ORDER BY [splitdata].[splitName] ASC))
          FROM splitdata;
          GO

          ;WITH
          permutations AS
          (
          SELECT
          ID = t.ID,
          FullNameVariation = CAST(t.NamePart AS varchar(500)),
          Level = COUNT(*) OVER (PARTITION BY t.ID)
          FROM
          dbo.NameParts AS t
          UNION ALL
          SELECT
          ID = t.ID,
          FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
          Level = p.Level - 1
          FROM
          dbo.NameParts AS t
          INNER JOIN permutations AS p ON t.ID = p.ID
          WHERE 1=1
          AND p.Level > 1
          AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
          )
          SELECT
          ID,
          REPLACE(FullNameVariation,'~','')
          FROM
          permutations
          WHERE 1=1 AND
          Level = 1
          ORDER BY
          ID,
          FullNameVariation
          OPTION (MAXRECURSION 5100);

          DROP TABLE [dbo].[tbltest]
          DROP TABLE [dbo].[NameParts]





          share|improve this answer















          Extending the original answer by Andriy M we can uniqify the strings at the insert phase with a dummy character (in my case I used ~), and remove that character later.



          In this case I chose to use REPLICATE passing in the dummy character '~' and the ROW_NUMBER function for the number of instances to replicate. This should ensure that all strings are unique.



          dbFiddle



          Full code



          create table tbltest
          (
          name varchar(50),
          addres varchar(100)
          );

          insert into tbltest values('Sam Mak John','Street 1 HNo 101 USA');
          insert into tbltest values('Donatella Nobatti','HNo 101 UK');
          insert into tbltest values('Sam Buca','Main Road B Block UAE');
          insert into tbltest values('Juan Soponatime','Hight Street CA');
          insert into tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
          insert into tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');
          GO

          CREATE TABLE dbo.NameParts
          (
          ID int NOT NULL,
          NamePart varchar(30) NOT NULL
          );

          --Table variable to identity column for each words
          DECLARE @Inter TABLE (id int identity(1,1), names varchar(max));

          --Insert into table variable
          INSERT INTO @Inter(names) SELECT name from TblTest;

          --Query to insert into NameParts table
          ;WITH splitdata
          AS
          (
          SELECT id,splitname
          FROM
          (
          SELECT *,Cast('<X>' + Replace(F.Names, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
          FROM @Inter F
          )F1
          CROSS apply
          (
          SELECT fdata.d.value('.', 'varchar(50)') AS splitName
          FROM f1.xmlfilter.nodes('X') AS fdata(d)
          ) O
          )
          INSERT INTO dbo.NameParts
          SELECT id,[splitdata].[splitName] + REPLICATE('~',(ROW_NUMBER() OVER (PARTITION BY [splitdata].[id] ORDER BY [splitdata].[splitName] ASC))
          FROM splitdata;
          GO

          ;WITH
          permutations AS
          (
          SELECT
          ID = t.ID,
          FullNameVariation = CAST(t.NamePart AS varchar(500)),
          Level = COUNT(*) OVER (PARTITION BY t.ID)
          FROM
          dbo.NameParts AS t
          UNION ALL
          SELECT
          ID = t.ID,
          FullNameVariation = CAST(p.FullNameVariation + ' ' + t.NamePart AS varchar(500)),
          Level = p.Level - 1
          FROM
          dbo.NameParts AS t
          INNER JOIN permutations AS p ON t.ID = p.ID
          WHERE 1=1
          AND p.Level > 1
          AND ' ' + p.FullNameVariation + ' ' NOT LIKE '% ' + t.NamePart + ' %'
          )
          SELECT
          ID,
          REPLACE(FullNameVariation,'~','')
          FROM
          permutations
          WHERE 1=1 AND
          Level = 1
          ORDER BY
          ID,
          FullNameVariation
          OPTION (MAXRECURSION 5100);

          DROP TABLE [dbo].[tbltest]
          DROP TABLE [dbo].[NameParts]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 9 at 16:06

























          answered May 9 at 8:45









          George.PalaciosGeorge.Palacios

          3,058827




          3,058827























              0














              You didn't mention number of rows return in output if Ahmed is repeated.



              In my script, 74 rows are return. It count Ahmed as one.



              If different Output is desire then show the Example of repeated word like 'Ahmed'



              Any Split string function that also return row number.



              CREATE FUNCTION [dbo].[DelimitedSplit2K]

              (@pString VARCHAR(2000), @pDelimiter CHAR(1))

              RETURNS TABLE WITH SCHEMABINDING AS
              RETURN

              WITH E1(N) AS (
              SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
              SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
              SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
              ), --10E+1 or 10 rows
              E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
              E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
              cteTally(N) AS (

              SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
              ),
              cteStart(N1) AS (
              SELECT 1 UNION ALL
              SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
              ),
              cteLen(N1,L1) AS(
              SELECT s.N1,
              ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
              FROM cteStart s
              )

              SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
              Item = SUBSTRING(@pString, l.N1, l.L1)
              FROM cteLen l
              ;


              Number Table,



              create table tblnumber(number int not null)
              insert into tblnumber (number)
              select ROW_NUMBER()over(order by a.number) from master..spt_values a
              , master..spt_values b
              CREATE unique clustered index CI_num on tblnumber(number)


              Sample Data,



              create table #tbltest
              (
              id int identity(1,1),
              name varchar(50),
              addres varchar(100)
              );

              insert into #tbltest values('Sam Mak John','Street 1 HNo 101 USA');
              insert into #tbltest values('Donatella Nobatti','HNo 101 UK');
              insert into #tbltest values('Sam Buca','Main Road B Block UAE');
              insert into #tbltest values('Juan Soponatime','Hight Street CA');
              insert into #tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
              insert into #tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


              The Scripts (WITHOUT RBAR),



              create table #tbltest1
              (
              id int identity(1,1),
              name varchar(50)
              ,GroupID int
              ,rownum int

              );
              insert into #tbltest1 (name,GroupID,rownum)
              select item,t.id, ca.ItemNumber from #tbltest t
              cross apply(select * from dbo.DelimitedSplit2K(t.name,' '))ca


              DECLARE
              @max integer =
              POWER(
              (
              SELECT COUNT(*)
              FROM #tbltest1 AS s
              )
              ,3
              ) ;


              ;With CTE as
              (
              SELECT s.id
              ,s.GroupID
              ,ltrim((select ' '+name from #tbltest1 t1 where t1.GroupID =s.groupid order by (CRYPT_GEN_RANDOM(c.n)) for xml PATH('') ))name
              FROM #tbltest1 AS s
              CROSS APPLY (
              select top (@max) number n FROM dbo.tblnumber
              )c

              )

              select distinct name from CTE
              --where groupid=1


              drop table #tbltest
              drop table #tbltest1


              How many rows will be process at one time ?



              How many rows are there is table ?



              Above all how many average word can be there in column Name






              share|improve this answer



























                0














                You didn't mention number of rows return in output if Ahmed is repeated.



                In my script, 74 rows are return. It count Ahmed as one.



                If different Output is desire then show the Example of repeated word like 'Ahmed'



                Any Split string function that also return row number.



                CREATE FUNCTION [dbo].[DelimitedSplit2K]

                (@pString VARCHAR(2000), @pDelimiter CHAR(1))

                RETURNS TABLE WITH SCHEMABINDING AS
                RETURN

                WITH E1(N) AS (
                SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ), --10E+1 or 10 rows
                E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
                E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
                cteTally(N) AS (

                SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
                cteStart(N1) AS (
                SELECT 1 UNION ALL
                SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
                cteLen(N1,L1) AS(
                SELECT s.N1,
                ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                FROM cteStart s
                )

                SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
                Item = SUBSTRING(@pString, l.N1, l.L1)
                FROM cteLen l
                ;


                Number Table,



                create table tblnumber(number int not null)
                insert into tblnumber (number)
                select ROW_NUMBER()over(order by a.number) from master..spt_values a
                , master..spt_values b
                CREATE unique clustered index CI_num on tblnumber(number)


                Sample Data,



                create table #tbltest
                (
                id int identity(1,1),
                name varchar(50),
                addres varchar(100)
                );

                insert into #tbltest values('Sam Mak John','Street 1 HNo 101 USA');
                insert into #tbltest values('Donatella Nobatti','HNo 101 UK');
                insert into #tbltest values('Sam Buca','Main Road B Block UAE');
                insert into #tbltest values('Juan Soponatime','Hight Street CA');
                insert into #tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
                insert into #tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


                The Scripts (WITHOUT RBAR),



                create table #tbltest1
                (
                id int identity(1,1),
                name varchar(50)
                ,GroupID int
                ,rownum int

                );
                insert into #tbltest1 (name,GroupID,rownum)
                select item,t.id, ca.ItemNumber from #tbltest t
                cross apply(select * from dbo.DelimitedSplit2K(t.name,' '))ca


                DECLARE
                @max integer =
                POWER(
                (
                SELECT COUNT(*)
                FROM #tbltest1 AS s
                )
                ,3
                ) ;


                ;With CTE as
                (
                SELECT s.id
                ,s.GroupID
                ,ltrim((select ' '+name from #tbltest1 t1 where t1.GroupID =s.groupid order by (CRYPT_GEN_RANDOM(c.n)) for xml PATH('') ))name
                FROM #tbltest1 AS s
                CROSS APPLY (
                select top (@max) number n FROM dbo.tblnumber
                )c

                )

                select distinct name from CTE
                --where groupid=1


                drop table #tbltest
                drop table #tbltest1


                How many rows will be process at one time ?



                How many rows are there is table ?



                Above all how many average word can be there in column Name






                share|improve this answer

























                  0












                  0








                  0







                  You didn't mention number of rows return in output if Ahmed is repeated.



                  In my script, 74 rows are return. It count Ahmed as one.



                  If different Output is desire then show the Example of repeated word like 'Ahmed'



                  Any Split string function that also return row number.



                  CREATE FUNCTION [dbo].[DelimitedSplit2K]

                  (@pString VARCHAR(2000), @pDelimiter CHAR(1))

                  RETURNS TABLE WITH SCHEMABINDING AS
                  RETURN

                  WITH E1(N) AS (
                  SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                  SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                  SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                  ), --10E+1 or 10 rows
                  E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
                  E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
                  cteTally(N) AS (

                  SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                  ),
                  cteStart(N1) AS (
                  SELECT 1 UNION ALL
                  SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                  ),
                  cteLen(N1,L1) AS(
                  SELECT s.N1,
                  ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                  FROM cteStart s
                  )

                  SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
                  Item = SUBSTRING(@pString, l.N1, l.L1)
                  FROM cteLen l
                  ;


                  Number Table,



                  create table tblnumber(number int not null)
                  insert into tblnumber (number)
                  select ROW_NUMBER()over(order by a.number) from master..spt_values a
                  , master..spt_values b
                  CREATE unique clustered index CI_num on tblnumber(number)


                  Sample Data,



                  create table #tbltest
                  (
                  id int identity(1,1),
                  name varchar(50),
                  addres varchar(100)
                  );

                  insert into #tbltest values('Sam Mak John','Street 1 HNo 101 USA');
                  insert into #tbltest values('Donatella Nobatti','HNo 101 UK');
                  insert into #tbltest values('Sam Buca','Main Road B Block UAE');
                  insert into #tbltest values('Juan Soponatime','Hight Street CA');
                  insert into #tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
                  insert into #tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


                  The Scripts (WITHOUT RBAR),



                  create table #tbltest1
                  (
                  id int identity(1,1),
                  name varchar(50)
                  ,GroupID int
                  ,rownum int

                  );
                  insert into #tbltest1 (name,GroupID,rownum)
                  select item,t.id, ca.ItemNumber from #tbltest t
                  cross apply(select * from dbo.DelimitedSplit2K(t.name,' '))ca


                  DECLARE
                  @max integer =
                  POWER(
                  (
                  SELECT COUNT(*)
                  FROM #tbltest1 AS s
                  )
                  ,3
                  ) ;


                  ;With CTE as
                  (
                  SELECT s.id
                  ,s.GroupID
                  ,ltrim((select ' '+name from #tbltest1 t1 where t1.GroupID =s.groupid order by (CRYPT_GEN_RANDOM(c.n)) for xml PATH('') ))name
                  FROM #tbltest1 AS s
                  CROSS APPLY (
                  select top (@max) number n FROM dbo.tblnumber
                  )c

                  )

                  select distinct name from CTE
                  --where groupid=1


                  drop table #tbltest
                  drop table #tbltest1


                  How many rows will be process at one time ?



                  How many rows are there is table ?



                  Above all how many average word can be there in column Name






                  share|improve this answer













                  You didn't mention number of rows return in output if Ahmed is repeated.



                  In my script, 74 rows are return. It count Ahmed as one.



                  If different Output is desire then show the Example of repeated word like 'Ahmed'



                  Any Split string function that also return row number.



                  CREATE FUNCTION [dbo].[DelimitedSplit2K]

                  (@pString VARCHAR(2000), @pDelimiter CHAR(1))

                  RETURNS TABLE WITH SCHEMABINDING AS
                  RETURN

                  WITH E1(N) AS (
                  SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                  SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                  SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                  ), --10E+1 or 10 rows
                  E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
                  E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
                  cteTally(N) AS (

                  SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                  ),
                  cteStart(N1) AS (
                  SELECT 1 UNION ALL
                  SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                  ),
                  cteLen(N1,L1) AS(
                  SELECT s.N1,
                  ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                  FROM cteStart s
                  )

                  SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
                  Item = SUBSTRING(@pString, l.N1, l.L1)
                  FROM cteLen l
                  ;


                  Number Table,



                  create table tblnumber(number int not null)
                  insert into tblnumber (number)
                  select ROW_NUMBER()over(order by a.number) from master..spt_values a
                  , master..spt_values b
                  CREATE unique clustered index CI_num on tblnumber(number)


                  Sample Data,



                  create table #tbltest
                  (
                  id int identity(1,1),
                  name varchar(50),
                  addres varchar(100)
                  );

                  insert into #tbltest values('Sam Mak John','Street 1 HNo 101 USA');
                  insert into #tbltest values('Donatella Nobatti','HNo 101 UK');
                  insert into #tbltest values('Sam Buca','Main Road B Block UAE');
                  insert into #tbltest values('Juan Soponatime','Hight Street CA');
                  insert into #tbltest values('Aaron Spacemuseum','HNo A10 100 feet Road A Block ');
                  insert into #tbltest values('Nasir Ahmed Amir Ahmed Mehendy','UAE High Street');


                  The Scripts (WITHOUT RBAR),



                  create table #tbltest1
                  (
                  id int identity(1,1),
                  name varchar(50)
                  ,GroupID int
                  ,rownum int

                  );
                  insert into #tbltest1 (name,GroupID,rownum)
                  select item,t.id, ca.ItemNumber from #tbltest t
                  cross apply(select * from dbo.DelimitedSplit2K(t.name,' '))ca


                  DECLARE
                  @max integer =
                  POWER(
                  (
                  SELECT COUNT(*)
                  FROM #tbltest1 AS s
                  )
                  ,3
                  ) ;


                  ;With CTE as
                  (
                  SELECT s.id
                  ,s.GroupID
                  ,ltrim((select ' '+name from #tbltest1 t1 where t1.GroupID =s.groupid order by (CRYPT_GEN_RANDOM(c.n)) for xml PATH('') ))name
                  FROM #tbltest1 AS s
                  CROSS APPLY (
                  select top (@max) number n FROM dbo.tblnumber
                  )c

                  )

                  select distinct name from CTE
                  --where groupid=1


                  drop table #tbltest
                  drop table #tbltest1


                  How many rows will be process at one time ?



                  How many rows are there is table ?



                  Above all how many average word can be there in column Name







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 15 at 11:00









                  KumarHarshKumarHarsh

                  98369




                  98369



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Database Administrators Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f237720%2fpermutation-and-combination-of-words%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

                      Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

                      What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company