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
 General SQL Server Forums
 New to SQL Server Programming
 Case inside a Join?

Author  Topic 

DaveBF
Yak Posting Veteran

89 Posts

Posted - 2014-03-04 : 11:56:53
Is it possible to do something like this:

Select a.address from person p 
join Addresses a on p.AddressPointer =
Case when @UseNewAddress=1 then a.NewAddress else a.OldAddress end


It gives a syntax error but I'm wondering if there's another way to do it.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-03-04 : 12:25:05
quote:
Originally posted by DaveBF

Is it possible to do something like this:

Select a.address from person p 
join Addresses a on p.AddressPointer =
Case when @UseNewAddress=1 then a.NewAddress else a.OldAddress end


It gives a syntax error but I'm wondering if there's another way to do it.

What kind of syntax error are you getting? I don't see anything wrong with the syntax as long as you have defined the @UseNewAddress variable and p.AddressPointer, a.NewAddress and a.OldAddress are compatible types.
Go to Top of Page

DaveBF
Yak Posting Veteran

89 Posts

Posted - 2014-03-04 : 12:30:38
quote:
Originally posted by James K

quote:
Originally posted by DaveBF

Is it possible to do something like this:

Select a.address from person p 
join Addresses a on p.AddressPointer =
Case when @UseNewAddress=1 then a.NewAddress else a.OldAddress end


It gives a syntax error but I'm wondering if there's another way to do it.

What kind of syntax error are you getting? I don't see anything wrong with the syntax as long as you have defined the @UseNewAddress variable and p.AddressPointer, a.NewAddress and a.OldAddress are compatible types.




Ooops! Sorry about this. You're right. I had something else wrong that was giving me a syntax error. I assumed it was because I couldn't do what I tried to do above. Thanks for the reply, James.
Go to Top of Page
   

- Advertisement -