How to Return Elements from a JSON Array in MariaDB

MariaDB includes two selectors that enable us to select elements from JSON arrays:

  • [N] selects element number N in the array (for example, [0] to select the first element).
  • [*] selects all elements in the array.

These can be used in a number of JSON functions that are included in MariaDB. The following examples use them with the JSON_EXTRACT() function in order to return selected array elements.

Continue reading

Fix Error Msg 4151 “The type of the first argument to NULLIF cannot be the NULL constant because the type of the first argument has to be known” in SQL Server

If you get error Msg 4151 “The type of the first argument to NULLIF cannot be the NULL constant because the type of the first argument has to be known” in SQL Server, it’s because you’re passing a null value as the first argument to the NULLIF() function.

To fix this error, make sure you do not pass the null constant as the first argument to the function. Or if you do, then convert it to a specific data type.

Continue reading

How to Create a Table Only if it Doesn’t Exist in SQLite

In SQLite, you can use the IF NOT EXISTS clause of the CREATE TABLE statement to check whether or not a table or view of the same name already exists in the database before creating it.

Creating a table without this clause would normally result in an error if a table of the same name already existed in the database. But when using the IF NOT EXISTS clause, the statement has no effect if a table already exists with the same name.

Continue reading

How to Remove the Right Padding on the Day Name in Oracle

In Oracle Database, when using the TO_CHAR() function to return the day name from a date, padding will be appended to the day name if it’s shorter than the longest valid day name for the given language and calendar.

Well, that’s the default behaviour. However, you can change this if you wish.

To suppress this padding, all you need to do is prepend the day name format element with fm.

Continue reading