Reputation: 549
I have 60 columns and I need to get unique values from all the columns using a SQL query. Is it possible in SQL Server?
Note I need to find distinct values from all the columns
Upvotes: 1
Views: 3109
Reputation: 24046
please try this code
declare @Sql_Str varchar(8000)='';
select @Sql_Str=@Sql_Str+' select cast (' +name +' as varchar(500)) from
<yourtable> union' from sys.columns where [object_id]=object_id('<yourtable>')
set @Sql_Str=SUBSTRING(@Sql_Str,1,len(@Sql_Str)-6)
exec(@Sql_Str)
Upvotes: 2
Reputation: 11
Upvotes: 0