SQL Formatting
DBCode provides flexible SQL code formatting powered by sql-formatter that can be customized per project using configuration files. This allows teams to maintain consistent SQL formatting across all developers and projects.
The SQL dialect is automatically detected based on your active database connection, so you don’t need to configure it manually.
Quick Start
Auto-Detection
DBCode automatically detects .sql-formatter.json files in your workspace folders:
- Create a 
.sql-formatter.jsonfile in your workspace root - Add your formatting preferences (see examples below)
 - Format SQL files - DBCode automatically uses your config
 
Manual Configuration Path
You can also specify a custom config file location:
- Open VS Code Settings
 - Search for 
dbcode.sqlFormatter.configPath - Set the path to your config file (absolute or relative to workspace root)
 
Priority Order
When formatting SQL, DBCode merges options in this order (later takes precedence):
- sql-formatter defaults
 - VS Code editor settings (
tabWidth,useTabs) .sql-formatter.jsonin workspace (if found)- File at 
dbcode.sqlFormatter.configPath(if set) - SQL dialect (automatically detected from your database connection)
 
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')Comma Position
Control where commas appear in SELECT lists:
{  "commaPosition": "after"}Options: "before", "after", "tabular"
Example (after):
SELECT  user_id,  email,  created_atFROM usersExample (before):
SELECT  user_id  , email  , created_atFROM usersExample (tabular):
SELECT user_id     , email     , created_at  FROM usersAlias Tabulation
Align column aliases:
{  "tabulateAlias": true}Example:
SELECT  user_id                          AS id,  email                            AS user_email,  DATE(created_at)                 AS signup_dateFROM usersSpacing 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;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.
Global Configuration
Use the dbcode.sqlFormatter.configPath setting to point to a shared config:
{  "dbcode.sqlFormatter.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