Product articles SQL Prompt AI
SQL Prompt AI Versus Smart…

SQL Prompt AI Versus Smart Quotes

SQL Prompt AI can repair certain types of broken SQL almost instantly, saving hours of clean-up time. Just prompt it clearly, check the results are exactly what you intended, and you're ready to run.

Guest post

This is a guest post from Louis Davidson. I am a SQL Server professional with over 30 years of experience in data modeling, SQL Server coding and internals, and ETL.

I’ve been part of the SQL Server community for many years, as a speaker and writer, and have an equally long-standing enthusiasm for Dollywood and DisneyWorld, as a self-confessed roller coaster addict.

Anyone who has ever copied SQL code from a blog archive, web page, or perhaps an eBook knows the pain of smart quotes and other types of ‘corrupted’ SQL formatting. You paste your code into SSMS, hit Execute, and it lights up with syntax errors.

As an author, I’ve learned to double-check these gremlins, but they still sneak in. MS Word’s autocorrect ‘fixed’ your straight quotes, or your CMS system quietly coalesced double dashes (–) into hyphens (-). Often, it just seems like part of some natural, entropic decay in code formatting, as code moves from one place to another, or websites get upgraded.

Luckily, SQL Prompt’s AI feature makes cleaning up this kind of formatting corruption surprisingly quick. Nine times out of ten, it works exactly as expected and will save you many hours of clean-up time. The tenth time, it still does a good job and saves you time, but you may find a few errors. As with any automation tool, if you’re using it at scale, you’ll also need a quick way to verify the result.

When smart quotes break string literals

Way back in the SQL Server 2008 days, I wrote about Row Constructors on SQLBlog.com. Years later, the blog post was migrated to Simple Talk and somewhere along the journey, the quotes got smart. Here is a section of the ‘corrupted’ code from that old article:

When I pasted this code into SSMS and tried to execute it, I got a barrage of syntax errors:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '-'.
Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '‘'.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near '‘'.
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near '‘'.
Msg 102, Level 15, State 1, Line 18
Incorrect syntax near '‘'.

SQL Server expects straight quotes (‘) but they had been replaced with curly quotes (‘ and ’). Combine that with the single-dash comments (- comment) and you have SQL that won’t run.

At this point, while manually fixing yet another curly quote, I wondered: Could SQL Prompt clean this mess up for me? Its trusted Format SQL (Ctrl+K, Ctrl+Y) command can’t help here because it needs runnable SQL before it can apply formatting. But Prompt AI is different.

Straighten out the smart quotes with SQL Prompt AI

I selected the block of broken code and prompted: “Fix this code so it will work”. You can also click Fix SQL, though I generally prefer to use explicit prompts.

Fixing broken SQL with Prompt AI

Seconds later, Prompt AI returned syntactically valid, neatly formatted T-SQL. It fixed the corrupted literals, and corrected the comment dashes:

I scanned the changes, executed the code, and had my table of values.

Tailor your prompt to get the right result

While my first task worked rather nicely, AI isn’t deterministic. Running the same prompt on the same code won’t always produce the same output. For example, a few times the AI decided to add a schema creation to the batch, just in case I wanted it. When it didn’t, and I asked it to, it typically added a primary key constraint as well!

As with any AI tool, the more precise your prompt, the fewer surprises you’ll see. “Create the Inventory schema if it doesn’t exist” makes only that requested change, whereas a more general request like “Fix this code so it runs” gives the AI much more latitude, and therefore much more variability. You always need to think of the AI as a valued team member, but one who sometimes makes a few too many assumptions as they do their work.

Smart quotes in metadata and data

After the first time I used SQL Prompt AI to remove some smart quotes, I went to LinkedIn and shouted hurrah for all to see. A commenter on this post then asked: what if the smart quotes are included as part of the data, or even in an object or column name? Could SQL Prompt AI tell the difference between what needed fixing and what was intentional?

These cases are rare in real code, but they illustrate why a quick verification step is a necessary part of any workflow, especially where AI is generating or modifying code. Here we have a very silly Sales table that has smart quotes in both an object name and in its data.

In SQL Server, it is perfectly legal to name objects like this, as long as the identifier is wrapped in square brackets. You can get away with some very ‘creative’ naming. The string, 'Jed’s first sale', is also valid, and we’ll assume the smart quote is exactly what the user intended.

So, what happens if this code is part of a script that is passed through Prompt AI for a “fix the smart quotes” clean-up, perhaps before recreating the database on the development server? I highlighted the code, hit Fix SQL and this is the result:

The code still runs, but in fixing the broader problems caused by the use of smart quotes, Prompt AI has also straightened any that appear in both identifiers and data.

[Jed’s Sales] is not the same column as [Jed's Sales]. Both can happily exist in the same database. Also, a string literal value like N'Jed’s first sale' is not the same as N'Jed''s first sale'. The curly apostrophe (’) is ASCII 146, while the straight apostrophe (‘) is ASCII 39.

This can cause some ‘silent mismatches’. If your table has 'Jed’s first sale' and a query predicate has WHERE Description = 'Jed''s first sale', it will return no rows. Similarly, WHERE [Jed's Sales] > 40 may not return rows.

Even when I explicitly prompted, “Fix this query, but don’t change any string values or column names” Prompt AI still replaced the smart quotes. In my testing, this was the only data-changing behavior I saw: it respected accented characters and non-ASCII data, but smart quotes were always “corrected”.

Always check AI’s homework

Most of the time, with precise prompting, Prompt AI will save you time and give you the results you expected. However, we need to keep an eye out for any subtle, unintended changes.

Doing this by eye, in a large script, is the SQL equivalent of playing Spot the Difference. Instead, we might, for example, use tools like SQL Compare and SQL Data Compare to check for any small, unwanted changes between the original table with the AI-modified version. Or we can check for any metadata changes using a query that compares the column metadata, as I’ve demonstrated in this post. It outputs the columns that do and do not match, using a FULL OUTER JOIN:

SillySales          SillySales2
------------------- -----------------
SalesId             SalesId
Jed’s Sales         NULL
NULL                Jed's Sales
Description         Description

A Prompt AI workflow

The biggest takeaway from all these examples is just how much time Prompt AI can save. Fixing corrupted SQL that would have taken me many minutes, sometimes hours, was done in seconds. In most cases, it got everything right, but as with any automated tool, the best results come from pairing its speed with the necessary checks and reviews. I spent a minute or two verifying the output, in each case, just as I would when reviewing code generated by a script, template, or another team member.

The second takeaway is that AI is not magic. It’s an algorithm, not a mind reader, and occasionally it will “fix” things that weren’t broken, especially if your prompts are loose. It’s also non-deterministic, meaning that the same prompt may produce slightly different changes, comments, or formatting each time.

As a result, we always need to inspect any AI-modified code and compare it with the original to make sure nothing meaningful was changed that we didn’t want to be changed. SQL Prompt’s own formatting tools make those differences a lot easier to spot.

The right workflow is something like this:

  1. Use AI for the heavy lifting. Let it correct syntax issues and clean up formatting but make sure it doesn’t alter the result in any unintended way. You can often refine your prompt to be more precise about what should and shouldn’t change, although even that isn’t guaranteed.
  2. Verify the output. Check the results like they came from a knowledgeable intern on your team, one who’s enthusiastic but still learning T-SQL and your business rules.
  3. Test the behavior. Run the code and make sure it still does what you intended. If object names or data values matter, confirm there are no subtle mismatches or silent changes to the results.

SQL Prompt AI is an excellent assistant for cleaning up mangled SQL, but like any assistant, it needs supervision.

Conclusion

AI tools like SQL Prompt can turn what used to be a frustrating manual cleanup into a quick, almost enjoyable fix. Whether you’re rescuing code corrupted by smart quotes or repairing a messy script copied from the web, it’s a huge time-saver that will frequently put a big smile on your face.

Just remember: AI doesn’t understand your intention, so take the time to verify its output, as you would for any tool or person that modifies code you are responsible for.

Tools in this post

SQL Prompt

Write, format, and refactor SQL effortlessly

Find out more