Format a Number as Currency in SQL

Some DBMSs have functions that allow us to format numbers as currency just by passing the appropriate format string. This converts the number to a string with the applicable currency symbol, group separator, and decimal point (if relevant).

Other DBMSs don’t make it that easy, and you need to do a bit of work first.

Below are examples of using SQL to format numbers as currency in some of the most popular DBMSs.

Continue reading

Oracle GROUP_CONCAT() Equivalent

Some RDBMSs have a GROUP_CONCAT() function that allows you to return a query column as a delimited list (for example, a comma separated list). MySQL and MariaDB are two that have such a function.

PostgreSQL and SQL Server have similar functions called STRING_AGG().

Oracle, on the other hand, has the LISTAGG() function that does pretty much the same thing (and perhaps more).

So you could say that LISTAGG() is Oracle’s GROUP_CONCAT() equivalent.

Continue reading

Find a String within a String in SQL

Most of the major DBMSs provide us with a way to find a string within a string using SQL. By this, I mean use a SQL query to find the position of a substring within a string.

There are several SQL functions that allow us to do this, including INSTR(), LOCATE(), POSITION(), and CHARINDEX(). The function you use will depend on your DBMS, and possibly whether or not you need to specify a starting position.

Continue reading