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

                      Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

                      Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

                      Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020