Fix “At least one of the arguments to COALESCE must be an expression that is not the NULL constant” in SQL Server

In SQL Server, if you’re getting error Msg 4127 that reads “At least one of the arguments to COALESCE must be an expression that is not the NULL constant”, it’s probably because all of your arguments to the COALESCE() expression are the NULL constant.

To fix this issue, make sure at least one argument is not the NULL constant.

Continue reading

Fix “date is incompatible with int” in SQL Server when Adding to or Subtracting from a Date

If you’re getting error message “Msg 206” that reads “Operand type clash: date is incompatible with int” in SQL Server while trying to add to (or subtract from) a date, it’s probably because you’re trying to perform arithmetic between an integer and a date value.

To fix this issue, either change the date value to a datetime value or use the DATEADD() function.

Continue reading

How to Get Values That Don’t Contain Numbers in SQL Server

If you have a column in a SQL Server database table that contains character data, but some rows also contain numbers within that data, you can use the following technique to return just those rows that don’t contain numbers.

Numbers can be represented by words and other symbols, but for the purpose of this article, “number” means “numerical digit”. So we’re finding values that don’t contain any numerical digits.

Continue reading

3 Ways to Delete Duplicate Rows in SQL Server while Ignoring the Primary Key

The following examples use T-SQL to delete duplicate rows in SQL Server while ignoring the primary key or unique identifier column.

More specifically, the examples delete duplicate rows but keep one. So, given two identical rows, one is deleted and the other remains. This is often referred to as “de-duping” the table, “deduplication” of the table, etc.

Continue reading