{"id":5066,"library":"sql-formatter","title":"SQL Formatter","description":"sql-formatter is a Python library designed to format SQL queries, enhancing readability and quick understanding through consistent indentation and casing. It aims to standardize SQL query writing, similar to how 'black' formats Python code. The library provides both a command-line interface and a Python API, and is actively maintained with frequent bug fixes and feature enhancements.","status":"active","version":"0.6.2","language":"en","source_language":"en","source_url":"https://github.com/PabloRMira/sql_formatter/tree/master/","tags":["sql","formatter","code-style","linting","database"],"install":[{"cmd":"pip install sql-formatter","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required Python version for the library.","package":"python","optional":false}],"imports":[{"symbol":"format_sql","correct":"from sql_formatter.core import format_sql"}],"quickstart":{"code":"from sql_formatter.core import format_sql\n\nexample_sql = \"\"\"\ncreate or replace table mytable as -- mytable example\nseLecT a.asdf, b.qwer, -- some comment here\nc.asdf, -- some comment there\nb.asdf2\nfrOm table1 as a\nleFt join table2 as b -- and here a comment\non a.asdf = b.asdf -- join this way \n\"\"\"\n\nformatted_sql = format_sql(example_sql)\nprint(formatted_sql)","lang":"python","description":"This quickstart demonstrates how to import the `format_sql` function and use it to format a multi-line SQL string."},"warnings":[{"fix":"Upgrade to version 0.6.2 or later, which includes specific fixes to address these parsing issues.","message":"SQL keywords embedded within table or variable names (e.g., 'select', 'and') could lead to incorrect formatting due to misinterpretation. [cite: Bugfixes in 0.6.2 release notes]","severity":"gotcha","affected_versions":"<0.6.2"},{"fix":"Ensure you are using version 0.6.1 or newer. If issues persist, consider using block comments (`/* ... */`) or utilizing the `/* sql-formatter-disable */` comments to explicitly exclude problematic sections from formatting.","message":"Line comments starting with `--` might prematurely terminate formatting for subsequent SQL statements on the same line or block. [cite: Bugfixes in 0.6.1 release notes]","severity":"gotcha","affected_versions":"<0.6.1"},{"fix":"For SQL containing templating, use the `paramTypes` configuration option to define custom regular expressions that treat these patterns as parameter placeholders during formatting. Example: `format(sql, paramTypes={'custom': [{'regex': r'\\{\\w+\\}'}]})`.","message":"The library does not inherently understand or format templating syntax (e.g., `SELECT {col} FROM {tablename}`). Such constructs might break formatting or be ignored.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refer to the latest documentation or use `sql-formatter --help` for the current CLI usage. The primary command is `sql-formatter <sql_file(s)>` with options like `--max-line-length`.","message":"Several older command-line interface (CLI) commands were deprecated and subsequently removed, potentially causing 'command not found' errors if older scripts are used. [cite: Maintenance in 0.5.4 release notes]","severity":"deprecated","affected_versions":">=0.5.4"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}