# SQLCODE -007: Illegal character in the statement

Source: https://wndsoftware.com/mainframe-errors/sqlcode-7

### SQLCODE -007: Illegal character in the statement

SQLCODE -007 (SQLSTATE 42601) means the SQL statement contains a character that isn't valid in SQL, outside a string constant or a name in double quotes. Db2 names the character in the message, but a hidden control character can look like a blank.


#### Other names for this code

Also written as `SQLCODE -7`, `SQL -007`, `SQLCODE=-7`, `SQLCODE = -007`, `db2 -007`, `sqlstate 42601`, `SQL0007N`, `statement contains the illegal character`.


### What it means

Db2 reads a statement by splitting it into tokens, such as keywords, names, numbers, operators and punctuation. Each token has to be built from characters in the SQL character set. Inside a string constant in apostrophes, or a name in double quotes, nearly any character is allowed. Outside them, a character that can't start or continue any token stops the parse at once. Db2 returns -007 and puts that character in SQLERRMC.

This is an earlier check than the one behind -104. With -104 every character is fine, but a word or symbol is in the wrong place. With -007 Db2 never got as far as the grammar.

The character in the message isn't always readable. A tab, a null byte or another control character may print as a blank, a dot or nothing at all, so the message seems to point at empty space. When that happens, the only reliable way to see the problem is to look at the statement in hex.


### Common causes

- SQL copied from a word processor, an email or a web page. These bring curly quotes, long dashes and non-breaking spaces that look normal on screen but aren't valid SQL characters.
- A tab or other control character in SQL read from a data set, often a member edited on a PC and uploaded in binary mode instead of text mode.
- A statement built by a program in a buffer that wasn't cleared first, so low-values or leftover bytes end up inside or after the SQL text.
- The length field of a VARCHAR statement buffer is longer than the real statement, so Db2 reads whatever follows it as part of the SQL.
- A code page mismatch between the tool that built the SQL and Db2, so a character such as the vertical bar in `||` arrives as a different byte.


### How to find the problem

- Read the character in the `DSNT408I` message, or in SQLERRMC if the program shows the SQLCA. If it looks like a blank, assume it's a control character.
- Open the SQL in ISPF edit and type `HEX ON`. Every byte is shown in hex under the text. A tab is X'05' in EBCDIC, and a null is X'00'.
- In ISPF edit, `FIND P'.'` finds characters that can't be displayed. It's the quickest way to locate them in a long member.
- For dynamic SQL, display the statement buffer in hex just before the PREPARE or EXECUTE IMMEDIATE, together with its length field.
- If the SQL came from a PC, ask how it was transferred. A binary transfer keeps tabs and PC line ends.


### How to fix it

- Retype the bad character in a plain editor. Replace curly quotes with straight apostrophes and long dashes with a minus sign.
- Remove control characters with an ISPF CHANGE command, for example `C P'.' ' ' ALL`, then check the result before you run it.
- Transfer SQL source to the mainframe in text mode so the characters are converted to EBCDIC.
- In programs that build SQL, set the buffer to spaces before moving the text in, and set the VARCHAR length to the real length of the statement.
- Make sure the terminal emulator or client uses the code page your system expects, so symbols like | and ¬ reach Db2 as the right bytes.


### IBM documentation

[IBM Db2 for z/OS documentation](https://www.ibm.com/docs/en/db2-for-zos/13.0.0). Use the documentation for your installed release.

This link opens the relevant IBM reference library or lookup instructions. Match the complete code and your installed product release; it is not a code-specific review.

[Report a correction to WND Software](https://wndsoftware.com/contact#message).

Technical reviewer: [Albert Joseph](https://wndsoftware.com/authors/albert-joseph), WND Software.

Last reviewed 2026-10-18 by the WND Software team. Written in our own words. For the full reference, check IBM’s Db2 for z/OS documentation for your release.


### Related codes

[SQLCODE -104 Illegal symbol in the statement Read →](https://wndsoftware.com/mainframe-errors/sqlcode-104)
[SQLCODE -010 String constant not terminated Read →](https://wndsoftware.com/mainframe-errors/sqlcode-10)
[SQLCODE -199 Illegal use of a keyword Read →](https://wndsoftware.com/mainframe-errors/sqlcode-199)
[SQLCODE -084 Unacceptable SQL statement Read →](https://wndsoftware.com/mainframe-errors/sqlcode-84)

### Questions


#### Why does the message show a blank as the illegal character?

Because the character can't be displayed. It's usually a tab, a null or another control byte. Look at the statement in hex to find it.


#### What is the difference between -007 and -104?

-007 means one character isn't valid SQL at all. -104 means the characters are fine but a word or symbol is in a place the grammar doesn't allow.


#### Is it the same on Db2 for LUW?

Yes. Db2 for LUW reports SQL0007N with SQLSTATE 42601. Its message also shows the text just before the bad character, which makes it easier to find.

[Db2 SQLCODEs](https://wndsoftware.com/mainframe-errors/db2-sqlcodes) · [SQLCODE -679](https://wndsoftware.com/mainframe-errors/sqlcode-679) · [SQLCODE -723](https://wndsoftware.com/mainframe-errors/sqlcode-723)

[← All error codes](https://wndsoftware.com/mainframe-errors/)
