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

时间:2018-12-21 15:24:49

标签: sql string postgresql datetime special-characters

单引号后,我无法在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;

2 个答案:

答案 0 :(得分:2)

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

所以您应该使用

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

答案 1 :(得分:1)

尝试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)