How do I format a SQL statement?
Category:
technology and computing
databases
-- Format Selected Query: To format a selected query(s) in set of query(s), select the query(s) to be formatted. Select Edit -> SQL Formatter -> Format Selected Query (or press Ctrl+F12). -- Format All Queries: To format the whole batch of queries entered in the SQL window.
Considering this, how do you format a SQL query?
If the name is the same as an SQL keyword, enclose the name within quotation marks. The name of an object in a database for a table or a column should be unique and not too long. Avoid special characters in the name like $, &, * , etc. (use only letters, numbers, and underscores)
Subsequently, one may also ask, what is format in SQL?
Definition and Usage. The FORMAT() function formats a value with the specified format (and an optional culture in SQL Server 2017). Use the FORMAT() function to format date/time values and number values. For general data type conversions, use CAST() or CONVERT().
So, of course here's some of my own recommendations on how to make SQL more readable.
- One thing per line. Only put a single column/table/join per line.
- Align your projections and conditions.
- Use column names when grouping/ordering.
- Comments.
- Casing.
- CTEs.
- Conclusion.