I have 2 tables as below, and insert data into ftable from f_table accordingly. I want to insert field_3 into field3 if field_3 exists, else insert field_4 into field3.
How do I do that? Much appreciate for any reply.
CREATE TABLE ftable
(
field1 int null,
field2 int null,
field3 char(1)
);
CREATE TABLE f_table
(
field_1 int null,
field_2 int null,
field_3 char(1),
field_4 char(1)
);
INSERT INTO ftable (field1, field2, field3)
SELECT (field_1, field_2, field_3)
FROM f_table;
Not sure what you really mean by if
field_3exists – since it’s part of the table, that column always exists…Guessing that you probably mean: if it’s
NOT NULL– in that case, use thisCASEstatement:Update: as per w0lf’s suggestion, you could also use:
Makes it a bit more compact – but a tad less readable, in my opinion. But this will work just fine – if
field_3is NULL, thenfield_4is inserted instead.