|
sql
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
IN keywork with > 1 column?
I'm playing with SQL server 2008 on my home project, after several years of using Oracle. Question is: is it possible to use IN with more than one column? select ADDRESS from PEOPLE where (FIRST_NAME, LAST_NAME) in ( select FIRST_NAME, LAST_NAME from SOME_OTHER_TABLE ) Is this not allowed? I can get it to work on 1 column: select ADDRESS from PEOPLE where (FIRST_NAME) in ( select FIRST_NAME from SOME_OTHER_TABLE ) but that doesn't make sense. Oracle can do as many columns as you like. Thanks! What is your overall objective? Do you want to find the people with
the same first names and last names? You could do it this way... select ADDRESS from PEOPLE where (FIRST_NAME + LAST_NAME) in ( select FIRST_NAME + LAST_NAME from SOME_OTHER_TABLE ) ....assuming no NULL values in either name field. But you would probably be better off with something like this for efficiency, as concatenating strings isn't very efficient and won't take advantage of existing indexes on those fields. SELECT People.FirstName, People.LastName, People.Address FROM PEOPLE INNER JOIN SOME_OTHER_TABLE S ON People.FirstName = S.FisrtName AND People.LastName = S.LastName -Eric Isaacs On Dec 20, 1:38 pm, Eric Isaacs <eisa***@gmail.com> wrote:
> What is your overall objective? Do you want to find the people with And how would you distinguish Joe Yikes from Joey Ikes ;). Unles it is> the same first names and last names? > > You could do it this way... > > select ADDRESS > from PEOPLE > where (FIRST_NAME + LAST_NAME) in > ( > select FIRST_NAME + LAST_NAME > from SOME_OTHER_TABLE > ) a case sensitive collation and you make sure first letters are uppercase. On Dec 21, 10:47 am, Alex Kuznetsov <alk***@gmail.com> wrote:
Show quoteHide quote > On Dec 20, 1:38 pm, Eric Isaacs <eisa***@gmail.com> wrote: In that case you would use another special character between the> > > What is your overall objective? Do you want to find the people with > > the same first names and last names? > > > You could do it this way... > > > select ADDRESS > > from PEOPLE > > where (FIRST_NAME + LAST_NAME) in > > ( > > select FIRST_NAME + LAST_NAME > > from SOME_OTHER_TABLE > > ) > > And how would you distinguish Joe Yikes from Joey Ikes ;). Unles it is > a case sensitive collation and you make sure first letters are > uppercase. names, for example '#'. On Dec 21, 10:56 am, DeanB <deanbrow***@yahoo.com> wrote:
Show quoteHide quote > On Dec 21, 10:47 am, Alex Kuznetsov <alk***@gmail.com> wrote: And you'll need a check constraint to guarantee that your special> > > > > On Dec 20, 1:38 pm, Eric Isaacs <eisa***@gmail.com> wrote: > > > > What is your overall objective? Do you want to find the people with > > > the same first names and last names? > > > > You could do it this way... > > > > select ADDRESS > > > from PEOPLE > > > where (FIRST_NAME + LAST_NAME) in > > > ( > > > select FIRST_NAME + LAST_NAME > > > from SOME_OTHER_TABLE > > > ) > > > And how would you distinguish Joe Yikes from Joey Ikes ;). Unles it is > > a case sensitive collation and you make sure first letters are > > uppercase. > > In that case you would use another special character between the > names, for example '#'. character cannot be stored in neither column... SQL Server does not support vector expressions in predicates. You would
have to use a different technique, for example EXISTS: SELECT address FROM People AS P WHERE EXISTS(SELECT * FROM OtherTable AS O WHERE P.first_name = O.first_name AND P.last_name = O.last_name); Note that depending on how Oracle handles NULLs this may have different results than what you get in Oracle (that is, if Oracle considers {x, NULL} equivalent to {x, NULL} then results will differ). On Dec 20, 2:40 pm, Plamen Ratchev <Pla***@SQLStudio.com> wrote:
Show quoteHide quote > SQL Server does not support vector expressions in predicates. You would The only difference to me is in query readability - to me the first> have to use a different technique, for example EXISTS: > > SELECT address > FROM People AS P > WHERE EXISTS(SELECT * > FROM OtherTable AS O > WHERE P.first_name = O.first_name > AND P.last_name = O.last_name); > > Note that depending on how Oracle handles NULLs this may have different > results than what you get in Oracle (that is, if Oracle considers {x, > NULL} equivalent to {x, NULL} then results will differ). > > -- > Plamen Ratchevhttp://www.SQLStudio.com example I had above is easier to read than any joins. I just couldn't get the two-field version to work. Thanks all
Other interesting topics
Cursor Logic
Still Restoring... Backup and restore database to another server Big deletion is filling transaction log SQL 2K Defrag Utility Recomendation? I HAVE A PROBLEM IN SQL Help on Sql Server 2005 job steps lost SSMS Open Table Include Column Headers LogFile size - general information Query timeout |
|||||||||||||||||||||||