How to Format a Number with Two Decimal Places in SQLite
We can use
printf() in order to format floats in SQLite 3.8.3 or greater.
SELECT printf("%.2f", col_name) AS col_name FROM table_name;
However, if we want to reference this field later in the query, we’ll have to cast it to a number in order to make any numerical comparisons.
SELECT printf("%.2f", col_name) AS short_col_name FROM table_name ORDER BY cast(short_col_name AS signed);
More SQL Articles
- How to Modify Table to Use Composite Primary Key in MySQL
- How to Order By Multiple Columns in SQL
- How to Count the Number of Rows with the Same Value
- How to Order a SQL String Column By Its Numeric Value
- How to Check if a Column is Null in MySQL
- How to Combine Columns Values Into a New Column in MySQL
- How to Drop Multiple Columns with ALTER TABLE in SQL
- What Stripe Data Do I Store in My Database?
- How To Query JSON Object with Unknown Keys in PostgreSQL
- How To Convert an Escaped JSON String in PostgreSQL to a JSON Object