Puoi farlo con la forza bruta:
select groupid,
(case when min(left(name, 10)) = max(left(name, 10)) then left(name, 10)
when min(left(name, 9)) = max(left(name, 9)) then left(name, 9)
when min(left(name, 8)) = max(left(name, 8)) then left(name, 8)
when min(left(name, 7)) = max(left(name, 7)) then left(name, 7)
when min(left(name, 6)) = max(left(name, 6)) then left(name, 6)
when min(left(name, 5)) = max(left(name, 5)) then left(name, 5)
when min(left(name, 4)) = max(left(name, 4)) then left(name, 4)
when min(left(name, 3)) = max(left(name, 3)) then left(name, 3)
when min(left(name, 2)) = max(left(name, 2)) then left(name, 2)
when min(left(name, 1)) = max(left(name, 1)) then left(name, 1)
end) as CommonPrefix
from t
group by groupid;
Se non ti piace così tanto a digitare, si può anche fare:
select groupid,
max(case when min(left(name, n.n)) = max(left(name, n.n)) then left(name, n.n) end)
from t cross join
(select 1 as n union all selet 2 union all select 3 . . .
) n
group by groupid;
(o utilizzare una clausola di where
per ottenere il informazioni per un gruppo.) Per questo esempio, continua ad aggiungere numeri interi alla sottoquery n
fino alla lunghezza che vuoi testare.
+1 Che è una buona! –
Conosci il numero esatto di caratteri comuni? – Chris
@crudolf, Se conoscessi il numero esatto di caratteri comuni, mi limiterei a "sottostringli" :) –