如何在 PostgreSQL 字符串中的单引号后添加分号?

How to add semicolon after a single-quote in a PostgreSQL string?

我无法在单引号后使用分号在 PostgreSQL 中创建字符串。 例如。我需要创建一个字符串,如:

Delhi is India's capital; It's a beautiful state

如何创建这样的字符串?

我尝试了以下方法:

select 'Delhi is India\'s capital; It\'s a beautiful state'

我收到以下错误:

Invalid operation: unterminated quoted string at or near "'Delhi is India\'s capital" 
Position: 8;

在 SQL 中,您不会使用反斜杠转义单引号,而是将它们加倍。分号不需要转义。

所以你应该使用

SELECT 'Delhi is India''s capital; It''s a beautiful state';

尝试Dollar-Quoted String Constants

knayak=# select $$Delhi is India's capital; It's a beautiful state$$  as s;
                        s
--------------------------------------------------
 Delhi is India's capital; It's a beautiful state
(1 row)