40

以下のコードを実行すると:

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

結果を返します。

TEST   3
tester 2

結果が次のようになるように、グループを大文字と小文字を区別する方法はありますか:

Test   1
TEST   1
test   1
tester 2
4

4 に答える 4

32

テキストをバイナリとしてキャストする (または大文字と小文字を区別する照合を使用する) 必要があります。

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))

照合を使用する:

Select Name Collate SQL_Latin1_General_CP1_CS_AS, COUNT(name)
From temp
Group By Name Collate SQL_Latin1_General_CP1_CS_AS
于 2012-06-08T16:32:58.837 に答える
23

大文字と小文字を区別する照合を使用できます。

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
于 2012-06-08T16:36:56.750 に答える