thinkhard.SI

Prompt repository Data & spreadsheets

Convert a SQL query from one database to another

A query written for one database has to run on another, and you need it converted with every behavior difference listed and a way to test it.

Registry no.
DAT-042
Collection
Data & spreadsheets
Laid out for
Any chat model
Score
10 out of 10
Blanks to fill
7
Added

Data & spreadsheetsAny chat

The prompt

Score 10 of 10, Excellent

Use it when A query written for one database has to run on another, and you need it converted with every behavior difference listed and a way to test it.

  • {{source_dialect}}
  • {{target_dialect}}
  • {{audience}}
  • {{query}}
  • {{table_definitions}}
  • {{query_purpose}}
  • {{versions}}
Full prompt and why it works
Prompt
Convert the SQL query below from {{source_dialect}} to {{target_dialect}}, so that it returns exactly the same rows and values on the new database and {{audience}} can switch over without a surprise in a report.

The query:
"""
{{query}}
"""

Table definitions or column types, where I have them:
"""
{{table_definitions}}
"""

What the query is used for: {{query_purpose}}
Database versions: {{versions}}

Give me:
1. A table of every difference you found, with the columns Original (quote the clause), What it does, Equivalent in {{target_dialect}}, Behavior to watch. Consider row limits (TOP, LIMIT, FETCH FIRST), date and time functions and intervals, string concatenation with NULLs, ISNULL, IFNULL and COALESCE, identifier quoting, boolean values, integer division, string aggregation, case sensitivity in comparisons, and implicit type casts.
2. The converted query in a code block, keeping my aliases, CTE names and layout, with a -- comment on each line whose behavior might differ.
3. Things to test before switching, as a numbered list of up to 8 checks, for example running both versions on the same data and comparing row counts and the sums of key columns, then testing NULLs, month ends, time zones and ties in ORDER BY.

Keep any text outside the table, the query and the list under 250 words.

Keep the logic identical. Do not optimize, restructure or rename anything unless {{target_dialect}} requires it, and mark every change it forces. A good conversion is one where the only differences between the two queries are the ones the table explains.

Watch for behavior that changes silently even when the query runs. For example, Oracle treats an empty string as NULL, SQL Server's DATEDIFF counts boundaries crossed rather than time elapsed, and MySQL may accept columns in SELECT that are neither grouped nor aggregated. Check each such case against the versions given.

If a function has no exact equivalent, give the closest one and add "-- CHECK:" with what may differ. If the result depends on a column's data type and the table definitions do not say, ask instead of assuming. If part of the result relies on a setting rather than syntax, such as the session time zone or the collation, say so.

Variables

{{source_dialect}}
The database the query runs on now, for example "SQL Server" or "MySQL".
{{target_dialect}}
The database it must run on, for example "PostgreSQL", "Snowflake" or "BigQuery".
{{audience}}
Who relies on the query's results, for example "the finance team's month-end report".
{{query}}
The full query, exactly as it runs today.
{{table_definitions}}
CREATE TABLE statements or a list of columns with types for the tables involved. Write "none" if you do not have them.
{{query_purpose}}
What the result feeds, for example "a dashboard of daily revenue by store".
{{versions}}
Versions of both databases, for example "SQL Server 2019 to PostgreSQL 16".

Why it works

  • Format. A table of differences, the converted query with comments and a test list let each change be traced from the original clause to a check.
  • What to avoid. The logic stays identical and any change the target database forces is marked, so a conversion is never also a quiet rewrite.
  • If unsure. Functions without an exact match carry a CHECK comment, and conversions that depend on a column type wait for the table definitions.

How to use it

  1. Press Fill in blanks and type your details into each blank. What you type stays in this browser.
  2. Copy the filled prompt into the AI tool you already use, such as ChatGPT, Claude or Gemini, or open it in the Grader or the Prompt Tester here.
  3. Before you rely on it, try it on three or four real examples. The Prompt Tester runs them all at once and shows which ones go wrong.
Use it from code

Get this prompt as JSON from the free REST API, with your own text in the blanks, or let an AI assistant fetch it through the MCP server. No key.

curl -s https://thinkhard.si/api/v1/prompts/sql-dialect-conversion

With the MCP server added to Claude, ChatGPT, Cursor or VS Code, call get_prompt with {"prompt": "sql-dialect-conversion"}, or just ask for it by name.