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:

  1. Create a .sql-formatter.json file in your workspace root
  2. Add your formatting preferences (see examples below)
  3. Format SQL files - DBCode automatically uses your config

Manual Configuration Path

You can also specify a custom config file location:

  1. Open VS Code Settings
  2. Search for dbcode.sqlFormatter.configPath
  3. 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):

  1. sql-formatter defaults
  2. VS Code editor settings (tabWidth, useTabs)
  3. .sql-formatter.json in workspace (if found)
  4. File at dbcode.sqlFormatter.configPath (if set)
  5. 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:

-- Before
select 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_at
FROM users
WHERE active = true

Example (tabularLeft):

SELECT user_id,
email,
created_at
FROM users
WHERE active = true

Logical Operators

Control newline placement for AND/OR operators:

{
"logicalOperatorNewline": "before"
}

Options: "before", "after"

Example (before):

SELECT *
FROM users
WHERE status = 'active'
AND created_at > '2024-01-01'
AND email_verified = true

Example (after):

SELECT *
FROM users
WHERE status = 'active' AND
created_at > '2024-01-01' AND
email_verified = true

Expression Width

Set maximum characters in parenthesized expressions:

{
"expressionWidth": 50
}

Example:

-- Short expressions stay on one line
SELECT * FROM users WHERE (status = 'active' AND verified = true)
-- Long expressions break to multiple lines
SELECT *
FROM users
WHERE (
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_at
FROM users

Example (before):

SELECT
user_id
, email
, created_at
FROM users

Example (tabular):

SELECT user_id
, email
, created_at
FROM users

Alias Tabulation

Align column aliases:

{
"tabulateAlias": true
}

Example:

SELECT
user_id AS id,
email AS user_email,
DATE(created_at) AS signup_date
FROM users

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: false
WHERE age >= 18 AND status = 'active'
-- denseOperators: true
WHERE age>=18 AND status='active'

newlineBeforeSemicolon: When true, places semicolons on separate lines

-- newlineBeforeSemicolon: false
SELECT * FROM users;
-- newlineBeforeSemicolon: true
SELECT * 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) or Option+Shift+F (macOS)

Format Selection:

  • Select code → Right-click → Format Selection
  • Or press Ctrl+K Ctrl+F (Windows/Linux) or Cmd+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