Most major RDBMSs provide ways for us to format numbers using SQL. Some provide only basic formatting capabilities, while others are more advanced.
Below are examples of formatting a number in some of the more popular RDBMSs.
Here are two ways to format a number as a percentage in SQLite.
In SQLite, we can use the PRINTF() function or FORMAT() function to format numbers with leading zeros.
Most of the major RDBMSs provide several options for concatenating two or more strings.
CONCAT() function, which concatenates its arguments.CONCAT_WS() that allows you to specify a separator that separates the concatenated strings.Below are examples of each method.
SQLite has a printf() function or format() function that allows us to format numbers according to a format string.
As from SQLite 3.18.0, it accepts a comma flag, which enables us to have comma separators at the thousands marks for integers.
Further work can be done to get it working with real/floating point numbers.
In SQLite, we can use the strftime() function to return datetime values in our chosen format.
Therefore, we can use it to extract the day, month, and year from a date.
Normally if you need to drop a foreign key in SQL, you’d use the ALTER TABLE statement. But if you’re using SQLite, that’s not an option.
In SQLite, you can drop a table with the DROP TABLE statement.
You can optionally add the IF EXISTS clause to suppress any errors that might occur if the table doesn’t exist.
Also, if the table is referenced by a foreign key, there are a few things to be aware of.
SQLite has a non-standard SQL extension clause called ON CONFLICT that enables us to specify how to deal with constraint conflicts.
In particular, the clause applies to UNIQUE, NOT NULL, CHECK, and PRIMARY KEY constraints.
This article provides examples of how this clause can be used to determine how to handle primary key constraint conflicts.
By “primary key constraint conflicts”, I mean when you try to insert a duplicate value into a primary key column. By default, when you try to do this, the operation will be aborted and SQLite will return an error.
But you can use the ON CONFLICT clause to change the way SQLite deals with these situations.
One option is to use this clause in the CREATE TABLE statement when creating the table. Doing that will determine how all INSERT operations are treated.
Another option is to use the clause on the INSERT statement whenever you try to insert data into the table. This allows you to take advantage of the clause even when the table wasn’t created with it. When you use this option, the syntax is different; you use OR instead of ON CONFLICT.
The examples on this page use the second option – I create the table without the ON CONFLICT clause, and I instead specify OR on the INSERT statement.