Convert SQL query results to CSV format
Convert SQL query results to CSV format instantly with our free online SQL to CSV converter. This tool helps database administrators and data analysts export SQL data to CSV for Excel and data analysis. Simply paste your SQL results and get CSV output in real-time.
Convert SQL query result tables to CSV (Comma-Separated Values) format for use in spreadsheets and other applications.
Supported input formats:
id | name | ageFeatures:
SQL to CSV converter transforms SQL query results into comma-separated values format. CSV is universally supported for data exchange, spreadsheet import, and analysis tools. Essential for reporting, data export, and integration with non-database systems.
First row contains column headers from SELECT columns. Each subsequent row contains values, comma-separated. Special characters (commas, quotes, newlines) are escaped by quoting fields. NULL values become empty fields or specified placeholder.
RFC 4180 defines CSV format: CRLF line endings, double-quote escaping, optional header row. Fields containing comma, quote, or newline must be quoted. Embedded quotes become double-quotes (""). Character encoding typically UTF-8.
Export database data for analysis in Excel or Google Sheets.
Universal format for sharing data between systems.
Generate reports that business users can open in spreadsheets.
Human-readable data backup format.
function escapeCSV(value) {
if (value === null || value === undefined) return '';
const str = String(value);
// Quote if contains comma, quote, or newline
if (/[",\n\r]/.test(str)) {
return '"' + str.replace(/"/g, '""') + '"';
}
return str;
}
function sqlToCSV(columns, rows) {
const header = columns.map(c => escapeCSV(c.name)).join(',');
const dataRows = rows.map(row =>
row.map(val => escapeCSV(val)).join(',')
);
return [header, ...dataRows].join('\n');
}
// Example
const columns = [{name: 'Name'}, {name: 'Description'}];
const rows = [
['Widget', 'A "great" product'],
['Gadget', 'Has, multiple, features']
];
sqlToCSV(columns, rows);
// Name,Description
// Widget,"A ""great"" product"
// Gadget,"Has, multiple, features"Escape fields containing special characters. Quote and double internal quotes.
Use SQL to CSV for spreadsheet exports, data exchange with non-technical users, reporting, and any situation requiring universal data format compatibility.
Options: empty field (most common), literal "NULL", or custom placeholder. Empty field is standard but may be ambiguous with empty strings. Document your choice.