Translate SQL Between MySQL, PostgreSQL and SQL Server with a Change Table

Returns your query rewritten for the target database, plus a table flagging every function, date, quoting and LIMIT change and where behavior could differ.

You getTranslated query in one code block, change table with columns and test checklist

Prompt

Prompt
You are a senior database engineer who migrates queries between MySQL, PostgreSQL, SQL Server, Oracle and SQLite.

I will paste a SQL query right after this prompt. Before translating, ask me in one numbered message: 1. the source dialect and version, 2. the target dialect and version, 3. whether results must match row for row, including NULLs, case sensitivity and ordering. Wait for my answers.

Rules:
- Never change syntax silently. Every change goes in the change table: function names, date and time math, string concatenation, identifier quoting, LIMIT/TOP/OFFSET, booleans, implicit casts.
- For each change, say whether behavior is identical or could differ, and give a concrete case where it differs (NULL in concatenation, collation, NULL sort order, time zones).
- Keep the original structure, aliases and comments so both versions diff line by line.
- If there is no direct equivalent, say so and give the closest option with its trade-off.
- Do not invent table definitions; mark any column type you rely on as an assumption.

Output format:
1. Translated query in one code block.
2. Change table with columns: # | Original | Translated | Category | Same behavior? | Edge case to test
3. Test checklist: 3-6 inputs or queries to run on both databases to confirm results match.

End with one line listing assumptions about data types, collation or server settings that could still change results.

Example output

At a glanceMySQL 8.0 to PostgreSQL 15: key changesSelected rows from the change table
ItemMySQL 8.0PostgreSQL 15
String concatenationCONCAT(first_name, ' ', last_name)double-pipe concatenation
Date formatDATE_FORMAT(created_at, '%Y-%m')TO_CHAR(created_at, 'YYYY-MM')
Null handlingIFNULL(discount, 0)COALESCE(discount, 0)
Date mathDATE_SUB(NOW(), INTERVAL 30 DAY)NOW() - INTERVAL '30 days'
ComparisonLIKEILIKE
PagingLIMIT 10, 20LIMIT 20 OFFSET 10

Example for: a MySQL 8.0 orders report moving to PostgreSQL 15, results must match row for row.

1. Translated query
-- Top recent orders from example.com customers
SELECT c.first_name || ' ' || c.last_name AS customer,
       o.order_total,
       TO_CHAR(o.created_at, 'YYYY-MM') AS month,
       COALESCE(o.discount, 0) AS discount
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.created_at >= NOW() - INTERVAL '30 days'
  AND c.email ILIKE '%@example.com'
ORDER BY o.order_total DESC NULLS LAST, o.id
LIMIT 20 OFFSET 10;
2. Change table
#OriginalTranslatedCategorySame behavior?Edge case to test
1CONCAT(first_name, ' ', last_name)double-pipe concatenationStringYes, chosen to matchPostgreSQL CONCAT skips NULLs; MySQL returns NULL. Test a customer with NULL last_name
2Backtick quoting on orders, oNo quotesQuotingYesMixed-case or reserved names would need double quotes
3DATE_FORMAT(created_at, '%Y-%m')TO_CHAR(created_at, 'YYYY-MM')DateYesNone for this format
4IFNULL(discount, 0)COALESCE(discount, 0)FunctionYesNone
5DATE_SUB(NOW(), INTERVAL 30 DAY)NOW() - INTERVAL '30 days'Date mathCould differNOW() returns timestamptz; compare against a timestamp column near midnight UTC
6LIKEILIKEComparisonYes, if MySQL collation is case-insensitiveEmail stored as 'Ann@EXAMPLE.com'
7ORDER BY order_total DESCAdded NULLS LAST and o.id tiebreakerOrderingCould differPostgreSQL puts NULLs first on DESC; ties paginate unpredictably on both
8LIMIT 10, 20LIMIT 20 OFFSET 10PagingYesConfirm row 11 is first on both
3. Test checklist
  • Insert a customer with NULL last_name; customer column should be NULL on both.
  • Insert an order with NULL order_total; it should appear last on both.
  • Create two orders with identical totals at the page boundary; confirm the same order IDs return.
  • Add an email in uppercase domain; it should match on both.
  • Run with session time zone set to America/New_York and compare row counts.

Assumptions: created_at is DATETIME in MySQL and timestamp without time zone in PostgreSQL; MySQL uses utf8mb4_0900_ai_ci collation; the o.id tiebreaker changes MySQL ordering slightly if you were relying on arbitrary tie order.

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Forcing every change into a table stops the model from silently rewriting functions whose behavior differs, like CONCAT with NULLs.
  • Asking whether results must match row for row decides tricky choices such as ILIKE versus LIKE and NULL sort order.
  • The test checklist turns flagged differences into concrete checks you can run on both databases before shipping.

Use this in ChatGPT, Claude or Gemini when porting reports, migrations or app queries to another database. Paste the full query and know both database versions and whether exact result parity matters.

Common edits

  • Paste the CREATE TABLE statements too so column types stop being assumptions.
  • Ask for a stored procedure or view translation instead of a single query.

Prompt details

By
Prompts-GPT editors
Published

Did this prompt work?

Keep exploring

More in Coding