--NickGivenCombine.sql
/*
2015-12-30 Tom Holden ve3meo
Appends the value from the Nickname field to the Given field and enclosed
in quotes. Nickname field cleared. 
Can be reversed using NickGivenSplit.sql.
*/

DROP VIEW IF EXISTS HasNick
;
CREATE TEMP VIEW IF NOT EXISTS HasNick AS
SELECT NameID FROM NameTable 
WHERE LENGTH(NickName)  -- Nickname field not empty
AND NOT INSTR(Given,'"')-- No embedded nickname in ""
AND NOT INSTR(Given,'(')-- No embedded nickname in ()
;
UPDATE NameTable 
SET Given = TRIM(Given) || ' ' || '"' || TRIM(Nickname) || '"'
    ,Nickname = ''
    WHERE NameID IN (SELECT NameID FROM HasNick)
    ;
