> This page location: PostgreSQL PL/pgSQL > Dollar-Quoted String Constants
> Full Neon documentation index: https://neon.com/docs/llms.txt

# Dollar-Quoted String Constants

**Summary**: in this tutorial, you will learn how to use the dollar-quoted string constants (`$$`) in user-defined functions and stored procedures.

## Introduction the dollar-quoted string constant syntax

In PostgreSQL, dollar-quoted string constants allow you to construct strings that contain single quotes without a need to escape them.

For example, you can surround a string constant using single quotes like this:

```plsql
select 'String constant';
```

But when a string constant contains a single quote (`'`), you need to escape it by doubling up the single quote:

```plsql
select 'I''m a string constant';
```

To make the code more readable, PostgreSQL offers a better syntax called dollar-quoted string constant or dollar quoting:

```plsql
select $$I'm a string constant$$;
```

In this example, we don't have to double up the single quote.

Here's the basic syntax of the dollar-quoted string constants:

```plsql
$tag$<string_constant>$tag$
```

In this syntax, the `tag` is optional. It follows the same rules as unquoted identifiers:

- Must begin with a letter (a-z, A-Z) or underscore.
- Can include letters (case-insensitive), digits, and underscores.
- Limited to 63 characters (longer ones are truncated).
- Cannot contain whitespaces, or reserved keywords without quotes.

Between the `$tag$`, you can place any string including single quotes (`'`). For example:

```plsql
select $$I'm a string constant$$ as message;
```

Output:

```text
        message
-----------------------
 I'm a string constant
(1 row)
```

In this example, we do not specify the `tag` between the two dollar signs(`$`).

The following example uses the dollar-quoted string constant syntax with a tag:

```plsql
SELECT $message$I'm a string constant$message$ s;
```

Output:

```text
           s
-----------------------
 I'm a string constant

```

In this example, we use the string `message` as a tag between the two dollar signs (`$` ).

## Using dollar-quoted string constants in anonymous blocks

The following shows the anonymous block in PL/pgSQL:

```plsql
do
'declare
   film_count integer;
begin
   select count(*) into film_count
   from film;

   raise notice ''The number of films: %'', film_count;
end;'
;
```

Note that you will learn about the anonymous block in the [PL/pgSQL block structure](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-block-structure) tutorial. In this tutorial, you can copy and paste the code in any PostgreSQL client tool like pgAdmin or psql to execute it.

Output:

```
NOTICE:  The number of films: 1000
DO
```

The code in a block must be surrounded by single quotes. If it has any single quote, you need to escape it by doubling it like this:

```text
 raise notice ''The number of films: %'', film_count;
```

To avoid escaping every single quotes and backslashes, you can use the dollar-quoted string as follows:

```plsql
do
$$
declare
   film_count integer;
begin
   select count(*) into film_count
   from film;
   raise notice 'The number of films: %', film_count;
end;
$$;
```

## Using dollar-quoted string constants in functions

The following shows the syntax of the [`CREATE FUNCTION`](https://neon.com/postgresql/postgresql-plpgsql/postgresql-create-function) statement that allows you to create a user-defined function:

```plsql
create function function_name(param_list)
    returns datatype
language lang_name
as
 'function_body'
```

Note that you will learn about the syntax of `CREATE FUNCTION` statement in the [creating function tutorial](https://neon.com/postgresql/postgresql-plpgsql/postgresql-create-function).

In this syntax, the `function_body` is a string constant. For example, the following function finds a film by its id:

```plsql
create function find_film_by_id(
   id int
) returns film
language sql
as
  'select * from film
   where film_id = id';
```

In this example, the body of the `find_film_by_id()` function is surrounded by single quotes.

If the function has many statements, it becomes more difficult to read. In this case, you can use dollar-quoted string constant syntax:

```plsql
create function find_film_by_id(
   id int
) returns film
language sql
as
$$
  select * from film
  where film_id = id;
$$;
```

Now, you can place any piece of code between the `$$` and `$$` without using the need to escape single quotes.

## Using dollar-quoted string constants in stored procedures

Similarly, you can use the dollar-quoted string constant syntax in [stored procedures](https://neon.com/postgresql/postgresql-plpgsql/postgresql-create-procedure) like this:

```plsql
create procedure proc_name(param_list)
language lang_name
as $$
  -- stored procedure body
$$
```

## Summary

- Use quoted-dollar string constant syntax to construct string constants without the need to escape single quotes.
- Do use quoted-dollar string constants in anonymous blocks, user-defined functions, and stored procedures.

---

## Related docs (PostgreSQL PL/pgSQL)

- [Introduction](https://neon.com/postgresql/postgresql-plpgsql/introduction-to-postgresql-stored-procedures)
- [Block Structure](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-block-structure)
- [Variables](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-variables)
- [Constants](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-constants)
- [IF-ELSE Statements](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-if-else-statements)
- [CASE Statement](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-case-statement)
- [Loop Statements](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-loop-statements)
- [FOR Loop](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-for-loop)
- [WHILE Loop](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-while-loop)
- [EXIT Statement](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-exit)
- [CONTINUE Statement](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-continue)
- [Cursor](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-cursor)
- [Function Parameters](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-function-parameters)
- [Function Overloading](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-function-overloading)
- [Function Returns Table](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-function-returns-a-table)
- [Returns SETOF](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-returns-setof)
- [Record Types](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-record-types)
- [Row Types](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-row-types)
- [Error Messages](https://neon.com/postgresql/postgresql-plpgsql/plpgsql-errors-messages)
- [Exception Handling](https://neon.com/postgresql/postgresql-plpgsql/postgresql-exception)
- [CREATE FUNCTION](https://neon.com/postgresql/postgresql-plpgsql/postgresql-create-function)
- [CREATE PROCEDURE](https://neon.com/postgresql/postgresql-plpgsql/postgresql-create-procedure)
- [DROP FUNCTION](https://neon.com/postgresql/postgresql-plpgsql/postgresql-drop-function)
- [DROP PROCEDURE](https://neon.com/postgresql/postgresql-plpgsql/postgresql-drop-procedure)
- [Stored Procedure with INOUT Parameters](https://neon.com/postgresql/postgresql-plpgsql/postgresql-stored-procedure-with-inout-parameters)
- [Introduction to Stored Procedures](https://neon.com/postgresql/postgresql-plpgsql/introduction-to-postgresql-stored-procedures)
- [SELECT INTO](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-select-into)
- [ASSERT Statement](https://neon.com/postgresql/postgresql-plpgsql/pl-pgsql-assert)
