Using a distinct query with a case-insensitive collation like SQL_Latin1_General_CP1_CI_AS will retrieve the first occurrence of any case-insensitive rows.
But I want to retrieve the lower case if both cases are present.
If only one case is present in the data I want to retrieve that case. (unique occurrences of capital case should remain in capital case in the result set)
Table:
id | col1
----------
1 | Ab
2 | ab
3 | cd
4 | Cd
5 | Ef
Query:
SELECT DISTINCT [col1] COLLATE SQL_Latin1_General_CP1_CI_AS
FROM dbo.table
Will retrieve: Ab, cd, Ef
But I want to retrieve: ab, cd, Ef
Are there collation that can give me my desired results?
Give priority to lower case instead of first case.
If not I have to use subselects.
- Select all distinct values using case-sensitive collation (strangely is automatically sorted, almost the use case I wanted!)
- Select from 1 using case-insensitive collation
May be something like this