SQL Formatting
Configure SQL code formatting in DBCode using VS Code settings or .sql-formatter.json files for consistent code style across your projects.
DBCode provides flexible SQL code formatting powered by sql-formatter with multiple configuration options. You can configure formatting through VS Code settings, workspace configuration files, or explicit config file paths.
The SQL dialect is automatically detected based on your active database connection, so you don’t need to configure it manually.
Quick Start
Option 1: VS Code Settings (Recommended)
The easiest and most discoverable way to configure SQL formatting:
- Open VS Code Settings (
Cmd/Ctrl + ,) - Search for “dbcode formatting”
- Configure your preferences (e.g.,
Keyword Case,Indent Style,Tab Width, etc.)
Settings can be configured at:
- User level (applies to all projects)
- Workspace level (applies to current project only)
Option 2: Configuration Files
For team-shared configurations or project-specific formatting rules:
Auto-Detection:
- Create a
.sql-formatter.jsonfile in your workspace root - Add your formatting preferences (see examples below)
- DBCode automatically detects and uses your config
Custom Path:
- Open VS Code Settings
- Search for “dbcode.formatting.configPath”
- Set the path to your config file (absolute or relative to workspace root)
Configuration Priority
When formatting SQL, DBCode merges options in this order (later takes precedence):
- sql-formatter defaults
- VS Code editor settings (
editor.tabSize,editor.insertSpaces) - DBCode formatting settings (
dbcode.formatting.*) - Workspace
.sql-formatter.jsonfile (if found) - Explicit config file at
dbcode.formatting.configPath(if set) - SQL dialect (automatically detected from your database connection)
This allows you to set global defaults in VS Code settings, override them per-project with .sql-formatter.json, and further override with an explicit config file if needed.
Configuration Options
Case Formatting
Control the casing of SQL keywords, data types, functions, and identifiers:
{ "keywordCase": "upper", "dataTypeCase": "upper", "functionCase": "upper", "identifierCase": "preserve"}Options: "preserve", "upper", "lower"
Example:
-- Beforeselect count(*) from users where created_at > now()
-- After (with upper case keywords/functions)SELECT COUNT(*) FROM users WHERE created_at > NOW()Indentation Style
Choose how SQL statements are indented:
{ "indentStyle": "standard", "tabWidth": 2, "useTabs": false}Indent Style Options:
"standard"- Standard block indentation"tabularLeft"- Align keywords to the left"tabularRight"- Align keywords to the right
Example (standard):
SELECT user_id, email, created_atFROM usersWHERE active = trueExample (tabularLeft):
SELECT user_id, email, created_at FROM users WHERE active = trueLogical Operators
Control newline placement for AND/OR operators:
{ "logicalOperatorNewline": "before"}Options: "before", "after"
Example (before):
SELECT *FROM usersWHERE status = 'active' AND created_at > '2024-01-01' AND email_verified = trueExample (after):
SELECT *FROM usersWHERE status = 'active' AND created_at > '2024-01-01' AND email_verified = trueExpression Width
Set maximum characters in parenthesized expressions:
{ "expressionWidth": 50}Example:
-- Short expressions stay on one lineSELECT * FROM users WHERE (status = 'active' AND verified = true)
-- Long expressions break to multiple linesSELECT *FROM usersWHERE ( status = 'active' AND email_verified = true AND created_at > '2024-01-01')Spacing Options
Control spacing and line breaks:
{ "linesBetweenQueries": 2, "denseOperators": false, "newlineBeforeSemicolon": false}linesBetweenQueries: Number of blank lines between separate queries
denseOperators: When true, removes spaces around operators
-- denseOperators: falseWHERE age >= 18 AND status = 'active'
-- denseOperators: trueWHERE age>=18 AND status='active'newlineBeforeSemicolon: When true, places semicolons on separate lines
-- newlineBeforeSemicolon: falseSELECT * FROM users;
-- newlineBeforeSemicolon: trueSELECT * FROM users;Column Alias Alignment
Align AS keywords in SELECT column lists for improved readability:
{ "tabulateAlias": true}When enabled, DBCode automatically aligns AS keywords and their aliases within SELECT clauses, making column lists easier to scan.
Example:
-- BeforeSELECT user_id AS id, email AS user_email, first_name AS fname FROM users
-- After (with tabulateAlias: true)SELECT user_id AS id, email AS user_email, first_name AS fnameFROM usersWorks with complex queries:
SELECT u.user_id AS id, u.email AS user_email, COUNT(o.order_id) AS order_count, SUM(o.total) AS total_spentFROM users uLEFT JOIN orders o ON u.user_id = o.user_idGROUP BY u.user_id, u.emailNote: This feature works best with single-line column expressions. Multi-line expressions (like complex subqueries) may not align as expected.
Complete Configuration Examples
Basic Configuration
Suitable for most teams - uppercase keywords, standard indentation:
{ "keywordCase": "upper", "dataTypeCase": "upper", "functionCase": "upper", "tabWidth": 2, "linesBetweenQueries": 2}Tabular Style
Great for aligned keywords:
{ "keywordCase": "upper", "dataTypeCase": "upper", "functionCase": "lower", "identifierCase": "preserve", "indentStyle": "tabularLeft", "logicalOperatorNewline": "before", "expressionWidth": 50, "linesBetweenQueries": 1, "tabWidth": 4, "useTabs": false}Compact Style
Minimalist formatting with lowercase keywords:
{ "keywordCase": "lower", "dataTypeCase": "lower", "functionCase": "lower", "indentStyle": "standard", "linesBetweenQueries": 1, "denseOperators": true, "tabWidth": 2}Using Formatting
Once your configuration is set up, format SQL code using:
Format Entire Document:
- Right-click → Format Document
- Or press
Alt+Shift+F(Windows/Linux) orOption+Shift+F(macOS)
Format Selection:
- Select code → Right-click → Format Selection
- Or press
Ctrl+K Ctrl+F(Windows/Linux) orCmd+K Cmd+F(macOS)
Multiple Workspaces
For monorepos or multi-project workspaces, place separate .sql-formatter.json files in each workspace folder. DBCode checks each workspace folder in order.
Using VS Code Settings
All formatting options can be configured through VS Code settings under the dbcode.formatting namespace:
dbcode.formatting.keywordCase- Case for SQL keywords (preserve/upper/lower)dbcode.formatting.dataTypeCase- Case for data types (preserve/upper/lower)dbcode.formatting.functionCase- Case for function names (preserve/upper/lower)dbcode.formatting.identifierCase- Case for identifiers (preserve/upper/lower)dbcode.formatting.indentStyle- Indentation style (standard/tabularLeft/tabularRight)dbcode.formatting.logicalOperatorNewline- AND/OR newline placement (before/after)dbcode.formatting.expressionWidth- Max chars in expressions (number)dbcode.formatting.linesBetweenQueries- Blank lines between queries (number)dbcode.formatting.denseOperators- Remove spaces around operators (boolean)dbcode.formatting.newlineBeforeSemicolon- Place semicolons on new lines (boolean)dbcode.formatting.tabulateAlias- Align AS keywords in SELECT lists (boolean)
Note: Tab width and tab/space preferences are automatically inherited from VS Code’s editor.tabSize and editor.insertSpaces settings.
Global Configuration File
Use the dbcode.formatting.configPath setting to point to a shared config file:
{ "dbcode.formatting.configPath": "~/shared-configs/sql-formatter.json"}Troubleshooting
Config not working:
- Verify JSON syntax is valid (use a JSON validator)
- Ensure file is named exactly
.sql-formatter.json - Check file is in workspace root or path is correct in settings
- Restart VS Code to reload the configuration
Inconsistent formatting:
- Multiple config files may exist (explicit path overrides workspace file)
- Verify options match sql-formatter documentation
- Check that your database dialect is supported
Configuration not reloading:
- DBCode watches for file changes automatically
- If changes don’t apply, try reloading VS Code window
Further Reading
- sql-formatter - The open-source formatter library powering DBCode’s SQL formatting
- sql-formatter configuration options - Complete reference for all available options
- SQL Editor - Learn about SQL editing features in DBCode
- Autocomplete - Intelligent SQL code completion