Skip to content

Document SQL literal syntax and escaping #14934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -2057,3 +2057,16 @@ select 1 where null between null and 2;
query T
select 'A' where null between 2 and null;
----

### Demonstrate use of E literals for escaping
# should not have literal tab
query T
select 'foo\t\tbar';
----
foo\t\tbar

# should have literal tab
query T
select E'foo\t\tbar';
----
foo bar
63 changes: 62 additions & 1 deletion docs/source/user-guide/sql/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
under the License.
-->

# Operators
# Operators and Literals

## Numerical Operators

Expand Down Expand Up @@ -552,3 +552,64 @@ Array Is Contained By
| true |
+-------------------------------------------------------------------------+
```

## Literals

Use single quotes for literal values. For example, the string `foo bar` is
referred to using `'foo bar'`

```sql
select 'foo';
```

### Escaping

Unlike many other languages, SQL literals do not by default support C-style escape
sequences such as `\n` for newline. Instead all characters in a `'` string are treated
literally.

To escape `'` in SQL literals, use `''`:

```sql
> select 'it''s escaped';
+----------------------+
| Utf8("it's escaped") |
+----------------------+
| it's escaped |
+----------------------+
1 row(s) fetched.
```

Strings such as `foo\nbar` mean `\` followed by `n` (not newline):

```sql
> select 'foo\nbar';
+------------------+
| Utf8("foo\nbar") |
+------------------+
| foo\nbar |
+------------------+
1 row(s) fetched.
Elapsed 0.005 seconds.
```

To add escaped characters such as newline or tab, instead of `\n` you use the
`E` style strings. For example, to add the text with a newline

```text
foo
bar
```

You can use `E'foo\nbar'`

```sql
> select E'foo\nbar';
+-----------------+
| Utf8("foo
bar") |
+-----------------+
| foo
bar |
+-----------------+
```