“SELECT DISTINCT” ignores different cases

后端 未结 4 1922
故里飘歌
故里飘歌 2020-12-03 06:17

I have the problem, that MSSQL Server 2000 should select some distinct values from a table (the specific column is of the nvarchar type). There are the sometimes the same va

相关标签:
4条回答
  • 2020-12-03 07:02

    Not sure about MS SQL but with MySQL or postgres, use BINARY for this operation. Cast the column to binary like so:

    SELECT DISTINCT BINARY(column1) from table1;
    

    Just change column1 and table1 as per your schema.

    Full example that works for me in MySQL 5.7, should work for others:

    SELECT DISTINCT BINARY(gateway) from transactions;
    

    Cheers!

    0 讨论(0)
  • 2020-12-03 07:03

    The collation will be set to case insensitive.

    You need to do something like this

    Select distinct col1 COLLATE sql_latin1_general_cp1_cs_as
    From dbo.myTable
    
    0 讨论(0)
  • 2020-12-03 07:04

    Try setting the collation of the column in question to something binary, e.g. utf8-bin. You can either do that in the SELECT statement itself or by changing your table structure directly (which means it doesn't have to map the collation each time the query is run, since it will store it correctly internally).

    0 讨论(0)
  • 2020-12-03 07:13
    SELECT DISTINCT
       CasedTheColumn 
    FROM
       (
       SELECT TheColumn COLLATE LATIN1_GENERAL_BIN AS CasedTheColumn
       FROM myTAble
       )FOO
    WHERE
       CasedTheColumn IN ('A', 'a'...)
    
    0 讨论(0)
提交回复
热议问题