2

I have 2 tables, table1 and table2, and I am using

insert into table1 
(  Col1,
   Col2,
   Col3
) 

select 
(  ColA,
   ColB,
   ColC
)
from 
table2

but the logic between Col3 and ColC is this:

if ColC = 'A' then Col3 = Y
else Col3 = N

What's the SQL for this, I am using SQL Server 2005.

4

1 回答 1

3

You can use a case expression:

insert into table1 
(  Col1,
   Col2,
   Col3
) 

select 
   ColA,
   ColB,
   case when ColC = 'A' then 'Y' else 'N' end
   -- alternatively:
   -- case ColC when 'A' then 'Y' else 'N' end
from 
table2
于 2012-06-05T02:41:18.310 回答