Using the distinct problem in case-sensitive SQL Server

Source: Internet
Author: User
Tags mixed

Problem

SQL Server provides you with the ability to store mixed-case data in your database, but depending on how you create the database, SQL Server ignores the case when you give T-SQL commands. One problem you may face is that you want to get a list that contains only a different value from a table, to show the difference in your table, but if your database is based on case-insensitive, then the distinct conditional statement will not show these differences, it will only put all the values together. So is there any choice in this area?

Expert answers

To illustrate this action, we will use a case-sensitive and case-insensitive database to see the two ways it works.

The first group of queries uses the AdventureWorks database, which is configured to be case-sensitive. To get a collection from your database, you can run this query:

SELECT name, collation_name
FROM master.sys.databases

We will query the Preson.contact data in the AdventureWorks database. All the data is created as a mixed case, so when we run this query we don't get duplicate values.

SELECT DISTINCT TOP 10 FirstName
FROM Person.Contact
WHERE FirstName LIKE 'A%'
ORDER BY 1

If you update a record and change FirstName from "Adam" to "Adam," then we get two different values when we run this query.

UPDATE Person.Contact
SET FirstName = 'ADAM'
WHERE ContactID = 62
GO
SELECT DISTINCT TOP 10 FirstName
FROM Person.Contact
WHERE FirstName LIKE 'A%'
ORDER BY 1

As you can see, "Adam" and "Adam" are now displayed as different values.

What we're going to do next is create a new table in a case-insensitive database and load all the data from person.contact into this new table.

CREATE TABLE Test.dbo.contact (FirstName nvarchar(50))
GO
INSERT INTO Test.dbo.contact
SELECT FirstName FROM Person.Contact
GO
SELECT DISTINCT TOP 10 FirstName
FROM Test.dbo.contact
WHERE FirstName LIKE 'A%'
ORDER BY 1
GO

When we run a select query, you can see that the output combines "Adam" and "Adam" because it is case-insensitive.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.