Oracle 10g+:
SELECT t.column1,
t.column2,
CASE
WHEN REGEXP_LIKE(t.column1, '^\w{3}') AND REGEXP_LIKE(t.column2, '\w{3}$') THEN
TRIM(t.column1) || ' ' || TRIM(t.column2)
WHEN REGEXP_LIKE(t.column1, '^\w{3}') AND REGEXP_LIKE(t.column2, '\s{3}\w{3}') THEN
TRIM(t.column1) || TRIM(LEADING FROM t.column2)
WHEN REGEXP_LIKE(t.column1, '\s{3}\w{3}') AND REGEXP_LIKE(t.column2, '\w{3}$') THEN
TRIM(TRAILING FROM t.column1) || TRIM(t.column2)
WHEN REGEXP_LIKE(t.column2, '^\w{3}') AND REGEXP_LIKE(t.column1, '\w{3}$') THEN
TRIM(t.column2) || ' ' || TRIM(t.column1)
WHEN REGEXP_LIKE(t.column2, '^\w{3}') AND REGEXP_LIKE(t.column1, '\s{3}\w{3}') THEN
TRIM(t.column2) || TRIM(LEADING FROM t.column1)
WHEN REGEXP_LIKE(t.column2, '\s{3}\w{3}') AND REGEXP_LIKE(t.column1, '\w{3}$') THEN
TRIM(TRAILING FROM t.column2) || TRIM(t.column1)
END AS col
FROM table1 t
Supporting scripts:
CREATE TABLE "EXAMPLE"."TABLE1" (
"COLUMN1" VARCHAR2(9 BYTE),
"COLUMN2" VARCHAR2(9 BYTE)
);
Insert into TABLE1 (COLUMN1,COLUMN2) values (' ABC ',' DEF');
Insert into TABLE1 (COLUMN1,COLUMN2) values ('ABC ',' DEF');
Insert into TABLE1 (COLUMN1,COLUMN2) values (' DEF ','ABC ');
Insert into TABLE1 (COLUMN1,COLUMN2) values (' DEF',' ABC ');
Insert into TABLE1 (COLUMN1,COLUMN2) values ('ABC ',' DEF ');
Insert into TABLE1 (COLUMN1,COLUMN2) values (' DEF','ABC ');