Tymek T. Tymek T. - 6 months ago 8
SQL Question

IF function in Oracle to insert data into tables

I want to make function which inserts 'F' in a field, when field "NAME" ends by '%a' and 'M' if not. How can I make this happen?

Answer

If the table is called t, and the column names are col_name and col_sex, and the string you enter for col_name is string_value, then what you want is:

insert into t (col_came, col_sex) values (string_value, 
                                case when string_value like '%a' then 'F' else 'M' end);
Comments