x

Pivot a column

I am trying to pivot a row and cant seem to figure out how. I am trying to combine two colums into one column with two rows. At first The names were all contained in one column but I wrote a function to spilt them up into seperate columns.

Here is a sample of the current data

ID MotherLastName   MotherFIrstName   FatherLastName FatherFirstName
 1      Smith           Mary                Smith         Joe

This is what I want

 ID  Lastname  FirstName
  1     Smith     Mary
  1     Smith     Joe
This is something I do often so a function would be ideal. Any suggestions?
more ▼

asked Mar 30, 2012 at 01:14 PM in Default

sqlLearner 1 gravatar image

sqlLearner 1
792 36 40 46

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

Based on your example, you just need a UNION :

select ID, MotherLastName as Lastname, MotherFirstName as Firstname
union all
select ID, FatherLastName, FatherFirstName
more ▼

answered Mar 30, 2012 at 01:17 PM

Kev Riley gravatar image

Kev Riley ♦♦
53.3k 47 49 76

Thanks that works!!..guess I was reading too much into the problem
Mar 30, 2012 at 01:37 PM sqlLearner 1
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x290
x81
x17

asked: Mar 30, 2012 at 01:14 PM

Seen: 741 times

Last Updated: Mar 30, 2012 at 01:37 PM