Fix “Arithmetic overflow error converting expression to data type int” in SQL Server

If you’re receiving error Msg 8115, Level 16, Arithmetic overflow error converting expression to data type int in SQL Server, it could be that you’re performing a calculation that results in an out of range value.

This can happen when you use a function such as SUM() on a column, and the calculation results in a value that’s outside the range of the column’s type.

Continue reading

Fix “Arithmetic overflow error converting IDENTITY to data type…” in SQL Server

If you’re getting error “Msg 8115, Level 16 Arithmetic overflow error converting IDENTITY to data type…” error in SQL Server, it’s probably because you’re trying to insert data into a table when its IDENTITY column has reached its data type’s limit.

An IDENTITY column automatically increments the value that’s inserted with each new row. If the value being inserted is out of the range of the column’s data type, then the above error will occur.

Continue reading

SQL LPAD()

In SQL, LPAD()is a commonly used function that pads the left part of a string with a specified character. The function can be used on strings and numbers, although depending on the DBMS, numbers may have to be passed as a string before they can be padded.

DBMSs that have an LPAD() function include MySQL, MariaDB, PostgreSQL, and Oracle.

DBMSs that don’t have an LPAD() function include SQL Server and SQLite (although there are other ways to apply left padding in these DBMSs).

Continue reading

SQL RPAD()

In SQL, RPAD()is used to pad the right part of a string with a specified character. The function can be used on strings and numbers, although depending on the DBMS, numbers may have to be passed as a string before they can be padded.

DBMSs that have an RPAD() function include MySQL, MariaDB, PostgreSQL, and Oracle.

DBMSs that don’t have an RPAD() function include SQL Server and SQLite.

Continue reading

Fix “Conversion failed when converting the varchar value” When Trying to Concatenate in SQL Server

If you get error Msg 245 that reads “Conversion failed when converting the varchar value…” when trying to perform a concatenation operation in SQL Server, it’s likely that you’re attempting to concatenate a string and a number.

Doing this will result in an error, due to SQL Server trying to add the string and number instead of concatenate them.

To fix this, either convert the number to a string, or use a function like CONCAT() or CONCAT_WS() to perform the concatenation.

Continue reading

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