Here's one piece of the puzzle, although a small one:IF OBJECT_ID('Tempdb.dbo.#T1', 'u') IS NOT NULLDROP TABLE #T1GOCREATE TABLE #T1(FullName VARCHAR(30))GOINSERT INTO #T1 VALUES ('Joe Smith')INSERT INTO #T1 VALUES ('April Smith ')INSERT INTO #T1 VALUES ('Peter Chen')INSERT INTO #T1 VALUES ('Kristine Nguyen')GOIF OBJECT_ID('Tempdb.dbo.#T2', 'u') IS NOT NULLDROP TABLE #T2GOCREATE TABLE #T2(FullName VARCHAR(30))GOINSERT INTO #T2 VALUES (' John Smith')INSERT INTO #T2 VALUES ('Lisa Tran ')INSERT INTO #T2 VALUES ('Nicole Ngo')INSERT INTO #T2 VALUES ('Brandon Lee')GOWITH nAS(SELECT n.numberFROM master..spt_values AS nWITH (NOLOCK)WHERE n.[type] = 'P' )select a.FullNameFROM #T1 aJOIN n ON n.number between 1 and LEN(a.FullName+':')-1where substring(a.fullname,n.number,1) = ' 'group by a.FullNamehaving COUNT(*) > 1union all select a.FullNameFROM #T2 aJOIN n ON n.number between 1 and LEN(a.FullName+':')-1where substring(a.fullname,n.number,1) = ' 'group by a.FullNamehaving COUNT(*) > 1/*FullName------------April Smith Joe Smith John SmithLisa Tran Nicole Ngo--*/Just as an example, you could use INFORMATION_SCHEMA.COLUMNS to find all the tables and columns matching a specific name or data type (varchar) and build the sql string like this:select 'union all select a.[' + COLUMN_NAME + '] FROM ' + TABLE_NAME + ' a JOIN n ON n.number between 1 and LEN(a.[' + COLUMN_NAME + ']+'':'')-1 where substring(a.[' + COLUMN_NAME + '],n.number,1) = '' '' group by a.[' + COLUMN_NAME + '] having COUNT(*) > 1'from INFORMATION_SCHEMA.COLUMNS where DATA_TYPE = 'varchar'
Again, just an example. You probably want to make a function and then build the sql string off the function.