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

avatar image

sqlLearner 1
972 43 51 57

(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

avatar image

Kev Riley ♦♦
66.8k 48 65 81

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.

Follow this question

By Email:

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



Answers and Comments

SQL Server Central

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



asked: Mar 30, 2012 at 01:14 PM

Seen: 903 times

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

Copyright 2018 Redgate Software. Privacy Policy