Here’s a list of units that can be used in MySQL datetime and interval functions.
Continue readingCategory: Relational
Format 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.
Concatenate a String and a Number in MySQL
There are a couple of approaches we can use to concatenate strings and numbers in MySQL.
- We can use the
CONCAT()
function, which concatenates its arguments. - We can use the pipe concatenation operator (
||
), which concatenates its operands.
Below are examples of each.
Continue reading7 Options for Enabling Pipes (||) as the Concatenation Operator in MariaDB
By default, two pipe characters (||
) are treated as a logical OR
operator in MariaDB. However, you can change this behaviour if you like.
You can update your sql_mode
to include the PIPES_AS_CONCAT
option, in which case two pipes will be treated as a concatenation operator.
There are quite a few ways to add this option to your sql_mode
. You can add it explicitly. Or you can set your sql_mode
to an option that sets it implicitly.
I run through these options below.
Continue readingHow 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 Enable the Pipe Concatenation Operator in MySQL
MySQL supports the use of the pipe concatenation operator (||
) for concatenating its operands. However, you need to enable it first.
Return the Day, Month, and Year in MySQL
MySQL has a bunch of different functions that enable us to get various date parts – such as the day, month, and year – from a date.
Continue readingYEAR() Examples – MySQL
In MySQL, YEAR()
is a built-in date and time function that returns the year from a given date expression.
It returns the year as a number in the range 1000
to 9999
. For zero dates, it could return 0
or NULL
with a warning, depending on the values in your sql_mode
.
Format a Number as Currency in MariaDB
Here’s an example of formatting a number as currency in MariaDB.
Continue readingHow to Format Numbers as Currency in MySQL
Some DBMSs provide us with the ability to format a number as a currency by providing a format specifier for the currency symbol. Providing such a format specifier allows the DBMS to return the appropriate currency symbol for the locale.
MySQL doesn’t have a currency format specifier, and so we need to do a bit of extra work if we want the currency symbol to be returned.
Continue reading