sql - How to place value in column A if it exist in another table, otherwise, place in column B -


i got lookup list table of valid state abbreviations , source table:

states lookup , source tables respectively

+-------+    +-----------------+ |states |    |id | location    | +=======+    +=================+ | ak    |    | 1 | madrid      | ---------    ------------------- | al    |    | 2 | ak          | ---------    ------------------- | ar    |    | 3 | ar          | ---------    ------------------- | ...   |    | ..| ...         | ---------    ------------------- 

how create insert statement target table such if location valid state, placed in state column , if it's not, placed in other locale column?

expected target table output

+------------+----------------- |id | state  | other locale   | +============+================= | 1 |        | madrid         | ------------------------------- | 2 | ak     |                | ------------------------------- | 3 | ar     |                | ------------------------------- | ..| ...    | ...            | ------------------------------- 

since nobody has suggested using nullif might throw in.

declare @location table (id int, location varchar(max)) declare @states table (states varchar(max)) insert @location values (1, 'madrid'), (2, 'ak'), (3, 'ar')  insert @states values ('ak'), ('al'), ('ar')   select  l.id,         s.states [state],         nullif(l.location,s.states) [other locale]    @location l         left join @states s on l.location = s.states 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -