In SQLite, we can use the PRINTF()
function or FORMAT()
function to format numbers with leading zeros.
Category: SQLite
How to Concatenate Strings in SQL
Most of the major RDBMSs provide several options for concatenating two or more strings.
- There’s the
CONCAT()
function, which concatenates its arguments. - There’s also a
CONCAT_WS()
that allows you to specify a separator that separates the concatenated strings. - And there’s also a string concatenation operator, which allows us to concatenate its operands.
Below are examples of each method.
Continue readingFormat Numbers with a Comma in SQLite
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.
Continue readingFormat a Number as Currency in SQLite
SQLite has a PRINTF()
function (and now a FORMAT()
function) that allows us to format a number based on a format string. For example, we can use this to format the number to a given number of decimal places, plus add a currency symbol to it.
How to Extract the Day, Month, and Year from a Date in SQLite
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.
Continue readingHow to Drop a Foreign Key in SQLite
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.
SQLite DROP TABLE
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.
Continue readingDealing with Primary Key Conflicts when Inserting Data in SQLite
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.
Set a Default Value for a Column in SQLite: DEFAULT Constraint
When creating a table in SQLite, you have the option of adding constraints to each column.
One such constraint is the DEFAULT
constraint.
The DEFAULT
constraint allows you to specify a value to be used in the event no value is supplied for that column when a new row is inserted.
If you don’t use a DEFAULT
clause, then the default value for a column is NULL
.
How iif() Works in SQLite
In SQLite, iif()
is a conditional function that returns the second or third argument based on the evaluation of the first argument.
It’s logically equivalent to CASE WHEN X THEN Y ELSE Z END
.
iif()
is an abbreviation for Immediate IF.
The iif()
function was introduced in SQLite 3.32.0, which was released on 22 May 2020.