COALESCE and Data Type Precedence

The COALESCE function is great for implementing elegant conditional logic in SQL Server queries and procedures.

But it can bite you. I didn’t fully understand the implications of this statement in the documentation until today:

Returns the data type of expression with the highest data type precedence.

SQL Server has many built-in data types. Books Online has a page that ranks them by precedence.

I found a blog post that documents this better than I can do just now. The jist is that COALSCE can fail if its arguments are of incomparable types, and the order of the parameters can determine whether two values are compared.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s