Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Is this possible?

Author  Topic 

dpnadam
Starting Member

29 Posts

Posted - 2010-11-18 : 11:48:09
Hi

Imagine I have a table called Details that looks like the below:

ID Animal1 Animal2 Animal3 Child1 Child2 Child3
1 Dog NULL NULL NULL Tom NULL
2 NULL NULL Cat Andrew NULL NULL
3 NULL Fish NULL NULL NULL Sarah
4 Horse NULL NULL NULL Laura NULL


I realise it does not make sense as a table, but this is similar to what I have to work with on a much reduced scale...

The query I am trying to do is retrieve just an animal and child value from each row. So in row 1 instead of getting each column I just want to get Dog AS Animal and Tom AS Child etc.

Is that possible?

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-18 : 11:51:34
yup possible

SELECT COALESCE(Animal1,Animal2,Animal3) AS Animal,COALESCE(Child1,Child2,Child3) AS Child FROM Table


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dpnadam
Starting Member

29 Posts

Posted - 2010-11-18 : 11:57:20
Wow that was quick. Works great, thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-11-18 : 12:00:05
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-11-18 : 12:11:11
quote:
Originally posted by dpnadam

Wow that was quick. Works great, thanks.



We ain't playin



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -