I am facing a strange issue while firing an SQL with CASE WHEN statements from within shell.
My SQL looks like:
insert into myschema.myTable (col1, col2, col3)
select col1,
CASE
WHEN col2 = 'NULL' THEN NULL
ELSE col2
END,
col3
from myschema.myTable_ext_table
(It is loading data from an external table that has some NULL values extracted as 'NULL' strings, and we cannot change the external table design)
My objective is to run psql with nohup so that big data loading tasks can run in the background. My psql command looks like this:
nohup sh -c 'date;psql -h myHAWQHost -d myHAWQDB --command "insert into myschema.myTable (col1, col2, col3) select col1, CASE WHEN col2 = 'NULL' THEN NULL ELSE col2 END, col3 from myschema.myTable_ext_table";date;' > myLog.out &
When I fire the SQL from any client (pgAdmin/Aginity) it works well. However when running from the psql command, it does not work, as the CASE WHEN is still putting 'NULL' strings in col2
column.
You have a quoting problem. In order to embed a single quote ('
) into a shell string which is also surrounded in single quotes, you need to switch to a double quoted string:
change:
cmd 'foo "bar 'baz' quux"'
# ^ ^
to:
cmd 'foo "bar '"'"'baz'"'"' quux"'
# ^^^^^ ^^^^^
You can use other escape styles, but this one is portable across all posix shells.