Logging Experiences

T-SQL: checking if a string is empty or white-space

Posted in Programming, SQL, Tips and Tricks by Sina Iravanian on September 6, 2012

Consider a case where you want to bring through a varchar column called SomeColumn from table SomeTable and replace its value with, 'Not Available' if the value is NULL or an empty string. This is how it can be achieved in Transact-SQL:

SELECT ISNULL(NULLIF(SomeTable.SomeColumn, ''), 'Not Available')
FROM SomeTable
...

The above code is checking for null or emptiness of the string. To check for being null or white-space, use the following code instead:

SELECT ISNULL(NULLIF(LTRIM(RTRIM(SomeTable.SomeColumn)), ''), 'Not Available')
FROM SomeTable
...
About these ads
Tagged with: , ,

2 Responses

Subscribe to comments with RSS.

  1. never cold call again ebook free download said, on February 25, 2013 at 8:59 pm

    Having read this I believed it was extremely enlightening.
    I appreciate you taking the time and effort to put this article together.

    I once again find myself spending a lot of time both reading and commenting.
    But so what, it was still worthwhile!

  2. www.online-dating-automator.com said, on April 9, 2013 at 3:09 am

    I am saving your internet site for near future studying!
    !


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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: