jay
jay

Reputation: 3889

how to check the condition in sql server?

Table 1

   slitem    firstname    lastname
    1         aaa           bbb

Table 2

slitem    firstname    lastname
 1         null           null
 2          null        null

Result :

slitem    firstname    lastname
    1         aaa           bbb
    2        null          null

i want to join this two table....

help me

Upvotes: 0

Views: 437

Answers (3)

Lucero
Lucero

Reputation: 60236

SELECT COALESCE(t1.slitem, t2.slitem) AS slitem, 
       COALESCE(t1.firstname, t2.firstname) AS firstname, 
       COALESCE(t1.lastname, t2.lastname) AS lastname
FROM      table1 t1 
FULL JOIN table2 t2 ON t1.slitem = t2.slitem

(Edit because OMG Ponies edited my FULL JOIN into a LEFT JOIN... which was not my intention!)

Upvotes: 2

IWriteApps
IWriteApps

Reputation: 993

You want a left join, just like the other answers said...but considering this is a rather basic thing to know when working SQL, you might want to read up a bit on it. Here's a good place to start.

Upvotes: 0

Mike Cheel
Mike Cheel

Reputation: 13106

It sounds like you are looking for a left outer join:

SELECT * FROM Table1 LEFT JOIN Table2 ON table2.slitem = Table1.slitem WHERE 1=1

Upvotes: 0

Related Questions