How to fix Error 2335 on Windows Server 2025 / Windows 11

Why This Error Happens

Error 2335 with the message “%sNewline in character/string constant” typically occurs when SQL Server’s parser encounters an unterminated string literal, often caused by a stray newline character or carriage return embedded within a quoted string in a T-SQL script. This commonly happens when scripts are copied from external sources (web pages, PDFs, or Unix-based editors) that introduce hidden line-break characters (CR/LF mismatches) inside string constants. Corrupted .sql files or improper encoding (such as UTF-8 without BOM being misread) can also trigger this parsing failure in SQL Server 2025.

Step-by-Step Resolution Guide

Step 1: Identify the Offending Script or Query

  • Open the .sql file in a plain-text editor like Notepad++ or VS Code rather than SSMS.
  • Enable “Show Line Endings” or “Render Whitespace” to visually inspect for stray \n or \r characters inside quotes.
  • Look specifically at lines surrounding the string literal referenced in the SSMS error output.

Step 2: Correct the String Literal Formatting

  • Ensure every string constant starts and ends on the same line, using a single pair of matching quotes ('...').
  • If a multi-line string is required, use the CHAR(13) + CHAR(10) concatenation method instead of a literal line break, for example:
    SET @msg = 'Line One' + CHAR(13) + CHAR(10) + 'Line Two';
  • Remove any invisible Unicode line separators (U+2028) that may have been pasted in from word processors.

Step 3: Normalize File Encoding and Line Endings

  • In VS Code or Notepad++, convert the file’s line-ending format to CRLF (Windows standard) via Edit > EOL Conversion > Windows (CR LF).
  • Re-save the file explicitly as UTF-8 with BOM to ensure SQL Server 2025’s parser reads it correctly.
  • If using sqlcmd on Windows Server 2025, re-run the script with the -f 65001 flag to force UTF-8 interpretation:
    sqlcmd -S servername -i script.sql -f 65001

Step 4: Re-run the Query in SQL Server Management Studio (SSMS)

  • Close and reopen the query window to clear any cached parse state.
  • Paste the corrected script into a fresh query tab rather than editing in place, as SSMS sometimes retains hidden formatting artifacts.
  • Execute the query again and confirm Error 2335 no longer appears in the Messages pane.

Alternative or Verification Steps

  • Run sp_helptext on the affected stored procedure (if applicable) to confirm the string constants render cleanly without embedded line breaks.
  • As an alternative, use PowerShell’s Get-Content script.sql -Raw combined with -replace "rn", " " to strip problematic line breaks before re-importing the script into SSMS.