Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How check if all data are the same in one column

Author  Topic 

duf
Starting Member

39 Posts

Posted - 2012-06-29 : 09:15:49
Hi. How to check if all data in one column are the same.Is there a simple query to do such job?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-06-29 : 09:17:34
select column, count(*) from table group by column


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2012-06-29 : 09:28:47
SELECT DISTINCT column FROM table; -- will show every distinct value in that column
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-06-29 : 09:34:19
quote:
Originally posted by duf

Hi. How to check if all data in one column are the same.Is there a simple query to do such job?



when you say the same, did you mean length, format or values???

explain please!



--------------------------
Get rich or die trying
--------------------------
Go to Top of Page

duf
Starting Member

39 Posts

Posted - 2012-06-29 : 09:36:47
So if there is more than one record it means that I have different data?
(replay to webfred)
Go to Top of Page

duf
Starting Member

39 Posts

Posted - 2012-06-29 : 09:37:52
quote:
Originally posted by xhostx

quote:
Originally posted by duf

Hi. How to check if all data in one column are the same.Is there a simple query to do such job?



when you say the same, did you mean length, format or values???

explain please!



--------------------------
Get rich or die trying
--------------------------


Values
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-06-29 : 09:41:54
My select shows you each existing value and a counter how often this value is existing.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

duf
Starting Member

39 Posts

Posted - 2012-06-29 : 09:42:15
quote:
Originally posted by jeffw8713

SELECT DISTINCT column FROM table; -- will show every distinct value in that column



The result is at least one record.
Go to Top of Page

duf
Starting Member

39 Posts

Posted - 2012-06-29 : 09:44:26
quote:
Originally posted by webfred

My select shows you each existing value and a counter how often this value is existing.


No, you're never too old to Yak'n'Roll if you're too young to die.


Thank you. But I would like to have a record only if a different value exists in the column
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-29 : 09:45:20
This is sort of rearranging/rehashing what others have already suggested, but if you are just looking for a boolean telling you whether or not all values are the same, then you can do something like this:
SELECT CASE 
WHEN COUNT(DISTINCT Col1) = 1 THEN 'All the same'
ELSE 'Not the same' END
FROM YourTable
That assumes you don't have null values in the column. If you do, you will need to decide whether that matters and if it does, you will need to make slight changes to the query.
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2012-07-02 : 15:35:51
quote:
Originally posted by duf

quote:
Originally posted by webfred

My select shows you each existing value and a counter how often this value is existing.


No, you're never too old to Yak'n'Roll if you're too young to die.


Thank you. But I would like to have a record only if a different value exists in the column



Different from what? Not sure I understand this requirement...can you post an example?
Go to Top of Page
   

- Advertisement -