x
login about faq Site discussion (meta-askssc)

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 '12 at 01:14 PM in Default

sqlLearner 1 gravatar image

sqlLearner 1
542 13 28 33

(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 '12 at 01:17 PM

Kev Riley gravatar image

Kev Riley ♦♦
46.1k 39 43 69

Thanks that works!!..guess I was reading too much into the problem

Mar 30 '12 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

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x224
x66
x10

asked: Mar 30 '12 at 01:14 PM

Seen: 370 times

Last Updated: Mar 30 '12 at 01:37 PM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.