Understanding the TRIM() Function in SQL Server

In SQL Server, we can use the TRIM() function to remove leading and trailing characters from a given string.

A common use case is to remove whitespace from the start and end of the string, but we can also specify other characters to remove.

Also, as from SQL Server 2022, we can specify which side of the string to remove the characters from (i.e. leading, trailing, or both).

Continue reading

Fixing the “data types” are “incompatible in the approx_percentile_cont operator” in SQL Server

If you’re getting SQL Server error msg 402 that tells you the “data types” are “incompatible in the approx_percentile_cont operator“, it’s probably because you’re trying to apply the APPROX_PERCENTILE_CONT() function against a non-numeric column (or one that doesn’t evaluate to a numeric type).

When using the APPROX_PERCENTILE_CONT() function, the ORDER BY expression in the WITHIN GROUP clause must evaluate to an exact or approximate numeric type. Other data types are not allowed, and will result in the above error.

To fix this issue, be sure to apply the function against a numeric column/expression.

Continue reading

Fix SQL Server Error: “The function ‘APPROX_PERCENTILE_CONT’ must have a WITHIN GROUP clause”

If you’re getting SQL Server error 10754 that reads “The function ‘APPROX_PERCENTILE_CONT’ must have a WITHIN GROUP clause” it’s probably because you’re calling the APPROX_PERCENTILE_CONT() function, but you’ve omitted the WITHIN GROUP clause.

To fix this issue, add a WITHIN GROUP clause to the function (and make sure it has an ORDER BY clause).

Continue reading