Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Hi all,
Can we divide a column of sql table into 3 more columns?
Like i have a column named Marks.
And i want to enter the marks of 10th, 12th, graduation under the column name Marks.
i.e. I have a primary column named 'Marks' and i want to create secondary columns named '10th', '12th', 'graduation' under it.
Is it possible?
If yes then how?
Posted
Comments
Abhishek Pant 20-Dec-12 0:55am    
hav you read about composite keys..
Skand Pratap Singh 20-Dec-12 0:59am    
yes..

Namaskar(Hi),

You can try this
1)create table emp(FirstName)
2)insert the values in that suppose i have 7 values
Sandeep
Prasad
Sharad
Sachin
Ravi
Ashish
Mangesh

and now i want to split my FistName column into 4 columns then
SQL
3);WITH T
     AS (SELECT firstname,
                ( ROW_NUMBER() OVER (ORDER BY (SELECT 0)) - 1 ) / 4 AS Row,
                ( ROW_NUMBER() OVER (ORDER BY (SELECT 0)) - 1 ) % 4 AS Col
         FROM   tempemp)
SELECT [0] AS Column1,
       [1] AS Column2,
       [2] AS Column3,
       [3] AS Column4
FROM   T PIVOT (MAX(firstname) FOR Col IN ([0], [1], [2], [3])) P 
ORDER BY Row

!!!!!!!!!!!!!!!!OUTPUT WILL BE!!!!!!!!!!!!!!!!!!
Column1  Column2  Column3   Column4
Sandeep	 Prasad	  Sharad    Sachin
Ravi	 Ashish	  Mangesh   NULL
 
Share this answer
 
Comments
Prasad_Kulkarni 20-Dec-12 8:13am    
Well answered +5!
yes, you can. You can select the same column three times, but perform operations on that value to extract your three values, and name them anything you like.
 
Share this answer
 
Comments
Devendra 1988 20-Dec-12 0:45am    
can you give a little more detailed explanation.I am nt exactly getting your point.
Christian Graus 20-Dec-12 1:31am    
Your question is vague, so my answer is vague, too. If you have one column that contains three bits of data, you can split that data within a select statement
Hi,

I can understand your requirement. I can suggest you to go like the following.

1. You can keep your column name as mark as it is.
2. Create another table that can contain your 10th, 12th, graduation marks. Keep one ID column in this table and set primary key to that column.
3. In your main table make the ID column of the second table to foreign key to Mark column.

In this way you can able to have more then one column under a single column.

Refer this link this will help you in dividing the column values.
Divide a column into many

Thanks
 
Share this answer
 
v2

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