I have a table in that I have only one column let say column name is XYZ and in that I have following values
a
a
A
a
A
A
a
I want a SQL query that should return me output as below:
a
A
I have a table in that I have only one column let say column name is XYZ and in that I have following values
a
a
A
a
A
A
a
I want a SQL query that should return me output as below:
a
A
You need to use case sensitive collation.
Following query will show you how to define collation for distinct select
select distinct XYZ COLLATE SQL_Latin1_General_CP1_CS_AS from [your table]
The "CS" at the end of collation name defines that operations on this column should be case sensitive.
You need to understand why you get a single row if distinct used and why the case sensitive does not work.
SQL Server is, by default, case insensitive; however, it is possible to create a case-sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine if a database or database object is to check its "COLLATION" property and look for "CI" or "CS" in the result.
Use collation for case sensitive enable or disable. There is different -2 type of level. (SQL Server check case-sensitivity?, How to do a case sensitive search in WHERE clause (I'm using SQL Server)?)
SQL Server check case-sensitivity?
Declare @tableA table ( cola varchar(50))
insert into @tableA values ('a'),('A'),('b'),('B')
select distinct cola from @tableA
--here you get both
select * from @tableA where cola = 'a'
--here you get only what you passed
select cola from @tableA where cola = 'a' COLLATE SQL_Latin1_General_CP1_CS_AS --Latin1_General_CS_AI--COLLATE Latin1_General_CS_AS
--here you get different result
select cola COLLATE SQL_Latin1_General_CP1_CS_AS from @tableA where cola = 'a'
You can also do:
select char(xyz) as xyz
from (select distinct ascii(xyz) as xyz from yourtable) a
If using ASCII character set.
select distinct(XYZ) from Letters
where Letters
is the name of table.