Interesting SQL ISNULL behavior

Found an interesting post about the SQL ISNULL function.  Apparently, it behaves differently if you’re using it to test strings.  For example:

DECLARE @a VARCHAR(100)
DECLARE @b VARCHAR(10)

SET @a=’This is much longer than 10 characters
SET @b=NULL

— This will display
—        This is mu

SELECT ISNULL(@b,@a)

— This will display
—        This is much longer than 10 characters

SELECT ISNULL(CONVERT(NVARCHAR(MAX), @b), @a)

Knowing this could keep you from pulling your hair out trying to investigate a bug when the truncated data gets down the pipeline.  Hope that helps someone out there.

Happy Coding!

Advertisement

About JohnHowell

I am a professional software developer with over 20 years of experience. I currently specialize in Microsoft technologies such as VS, TFS, C#, VB.Net, WCF, WPF, WW, etc.
This entry was posted in Uncategorized. Bookmark the permalink.

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 )

Facebook photo

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

Connecting to %s