web-dev-qa-db-fra.com

Comment faire un GROUP BY sensible à la casse?

Si j'exécute le code ci-dessous:

with temp as
(
  select 'Test' as name
  UNION ALL
  select 'TEST'
  UNION ALL
  select 'test'
  UNION ALL
  select 'tester'
  UNION ALL
  select 'tester'
)
SELECT name, COUNT(name)
FROM temp
group by name

Il renvoie les résultats:

TEST   3
tester 2

Existe-t-il un moyen de faire en sorte que le groupe soit sensible à la casse afin que les résultats soient:

Test   1
TEST   1
test   1
tester 2
29
Abe Miessler

Vous pouvez utiliser un classement sensible à la casse:

with temp as
(
  select 'Test' COLLATE Latin1_General_CS_AS as name
  UNION ALL
  select 'TEST'
  UNION ALL
  select 'test'
  UNION ALL
  select 'tester'
  UNION ALL
  select 'tester'
)
SELECT name, COUNT(name)
FROM temp
group by name
17
Lamak

Vous devez convertir le texte en binaire (ou utiliser un classement sensible à la casse).

With temp as
(
  select 'Test' as name
  UNION ALL
  select 'TEST'
  UNION ALL
  select 'test'
  UNION ALL
  select 'tester'
  UNION ALL
  select 'tester'
)
Select Name, COUNT(name)
From temp
Group By Name, Cast(name As varbinary(100))

Utilisation d'un classement:

Select Name Collate SQL_Latin1_General_CP1_CS_AS, COUNT(name)
From temp
Group By Name Collate SQL_Latin1_General_CP1_CS_AS
24
Thomas

Simplement:

SELECT count(*), CAST(lastname as BINARY) AS lastname_cs 
FROM names 
GROUP BY lastname_cs; 
10
Jordan DjMo