Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping single quotes in REDSHIFT SQL

I've lots of string values containing single quotes which I need to insert to a column in REDSHIFT table. I used both /' and '' to escape the single quote in INSERT statement.

e.g.

INSERT INTO table_Temp
    VALUES ('1234', 'O\'Niel'), ('3456', 'O\'Brien')

I also used '' instead of \' but it keeps giving me error that "VALUES list must of same length" i.e. no: of arguments for each record >2.

Can you let know how to have this issue resolved?

like image 379
stack_pointer is EXTINCT Avatar asked Dec 28 '17 00:12

stack_pointer is EXTINCT


2 Answers

The standard in SQL is double single quotes:

INSERT INTO table_Temp (col1, col2)  -- include the column names
    VALUES ('1234', 'O''Niel'), ('3456', 'O''Brien');

You should also include the column names corresponding to the values being inserted. That is probably the cause of your second error.

like image 170
Gordon Linoff Avatar answered Nov 17 '22 03:11

Gordon Linoff


You could use CHR(39) and concat the strings. Your name would look like below:

  ('O' || CHR(39)||'Brian')
like image 30
Anthony Kwiatkowski Avatar answered Nov 17 '22 03:11

Anthony Kwiatkowski