Select ID from #Temp where Left(id,6) IN (Select left(id,6) from #Temp group by left(id,6) having count(*) > 1)
SELECT ID --I'm assuming the column name is ID, substitute the true column nameFROM TableName t1 --I'm giving your table this generic table name, substitute the true table nameWHERE EXISTS ( SELECT * FROM TableName t2 --substitute the true table name here as well WHERE LEFT(t2.ID,6) = LEFT(t1.ID,6) --substitute the true column name here as well AND t2.ID <> t1.ID )ORDER BY ID
DECLARE @T TABLE (ProductID VARCHAR(50))INSERT INTO @TSELECT '003223' UNION ALL SELECT '003225' UNION ALL SELECT '003227'UNION ALL SELECT '003227A' UNION ALL SELECT '003236' UNION ALL SELECT '003236A'UNION ALL SELECT '003241' UNION ALL SELECT '003273' UNION ALL SELECT '003273A'SELECT ProductIDFROM ( SELECT ProductID, m=MAX(n) OVER (PARTITION BY LEFT(ProductID, 6)) FROM ( SELECT ProductID ,n=ROW_NUMBER() OVER (PARTITION BY LEFT(ProductID, 6) ORDER BY (SELECT NULL)) FROM @T) a) bWHERE m > 1