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)
 IF....THEN...???

Author  Topic 

mlevier
Starting Member

33 Posts

Posted - 2007-09-19 : 12:27:57
I have a problem that I don't know too much about. I am trying to pull records from one table. Sometimes that record is null and I need to use information from another field. How can I use one record if it isn't null but if it is then use a record from another field? I'm sure that this is simple to do but I haven't had much experience with it yet.I have tried things with if then statements but I'm stuck somewhere.

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-19 : 12:31:36
COALESCE or ISNULL functions will work for you.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

mlevier
Starting Member

33 Posts

Posted - 2007-09-19 : 12:40:40
How can you return a value that is in another field? I don't want a standard statement for all of them. I just want another record's values.

SELECT code, name,
COALESCE(leader, '?')
FROM party
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-09-19 : 12:44:50
Like this if the other column is from same table..:

SELECT code, name,
COALESCE(leader, follower)
FROM party



Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-19 : 12:49:15
Or any other table present in query

SELECT t1.Code, t1.Name, COALESCE(t2.Col1, t1.Col1) AS Office
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 ON t2.UserID = t1.OfficerID



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -