Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I have one table.I want tha column names based on column values

SQL
|Field1 |Field2 |Field3 |Field4 |
|-------|-------|-------|-------|
| 1     | 0     | 0     | 1     |

now i want the field name contain zero.Is this POssible in sql server.

What I have tried:

I've been trying to do various joins against sys.columns (and sys.tables), but so far to little avail.
Posted
Updated 5-Jan-17 21:00pm

Even if possible, modifying the column names based on the values on the data is not typically done. This would make the coding on the calling side much harder since the program doesn't know the names anymore so referencing the columns in the result set becomes harder, in some cases even impossible.

Another thing is that what if the result set contains two rows and the values for the same column are different on these rows? What should the column name be in such case?

[Edit:]
One way to do this would be to fetch the data and based on that create the query with custom column names.

But in order to do that you would need to fetch the data one time for deciding the column names and then fetch the data again to have it with custom column names. As pointed out this wouldn't make sense. It would be much easier for example to fetch the data to the client and then modify the display names as needed...

To demonstrate one variation how this could be achieved consider the following. But as said, I really don't recommend this.

Create the test data
SQL
CREATE TABLE ColumNameTest (
col1 int,
col2 int);

INSERT INTO ColumNameTest (col1, col2) VALUES
(1,2),
(3,null),
(null,6);

Create and run the query
SQL
DECLARE @query nvarchar(max)
DECLARE @alias nvarchar(max)
SET @query = ' SELECT '

SELECT @alias = STUFF((SELECT ',' + COALESCE(CAST(col1 AS nvarchar(100)), 'null') FROM columnametest FOR XML PATH('')),1,1,'');
SET @query = @query + ' col1 AS [' + @alias + ']'

SELECT @alias = STUFF((SELECT ',' + COALESCE(CAST(col2 AS nvarchar(100)), 'null') FROM columnametest FOR XML PATH('')),1,1,'');
SET @query = @query + ', col2 AS [' + @alias + ']'

SET @query = @query + ' FROM ColumNameTest'
PRINT @query
EXEC sp_executesql @query

The dynamically built query would look like
SQL
SELECT  col1 AS [1,3,null], col2 AS [2,null,6] FROM ColumNameTest

and the result is
1,3,null   2,null,6
--------   --------
1          2
3          NULL
NULL       6
 
Share this answer
 
v2
Comments
Member 12857356 6-Jan-17 2:57am    
So u say it is not possible .?
Wendelius 6-Jan-17 3:47am    
In order to answer your comment if I'm saying that this is impossible. No it's not but in most of the situations this would be a bad approach. Have a look at the updated answer.
Not everything that can be done should be done. Why do you want to do that? Imagine you have a series of data without any heading, e.g. [1,0,0,4] what does each element represent?
Another scenario, how do you store your contacts in the phone, do you store just phone number or contact name and phone number? Why the latter? You got the answer. The conclusion is don't do it.
 
Share this answer
 
v2
Comments
Member 12857356 6-Jan-17 3:03am    
Thanks, \

But in my case i want the Column name based n values ...
Peter Leow 6-Jan-17 3:08am    
Unless you are looking for https://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900