sql server replace blank with null in select

Hi,

Does anyone know a way to replace '' (blank) with NULL in the select statement without using a case statement?

i.e.
convert this to a select statement using replace, stuff, or?

SELECT CASE AccountId
                       WHEN ''
                           THEN NULL
                       ELSE AccountId
             END  AS AccountId
  FROM Accounts


 
ssebringAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HuyBDCommented:
try this
SELECT CASE WHEN AccountId=''
       THEN NULL
       ELSE AccountId
       END  AS AccountId
  FROM Accounts

Open in new window

0
Lee SavidgeCommented:
Hi,

select isnull(mycolumn, '') from mytable

Regards,

Lee
0
YiogiCommented:
This would be much more efficient:

SELECT NullIf(AccountId, '') FROM Accounts
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

YiogiCommented:
Lee he wants to do the exact oposite of what you posted :)
0
Lee SavidgeCommented:
Hi,

Sorry, misread the question there. Try this:

select replace(mycolumn, '', null) from mytable

Regards,

Lee
0
HuyBDCommented:
Yiogi, Lee just convert null to '', not '' to null value
Lee, it return null even value of field differ from '' (e.g 'a b')
0
ssebringAuthor Commented:
I forgot that NullIf was available!  It's been a long time since I needed it.  Thanks!
0
YiogiCommented:
Guys why don't you just use what I posted? It will work lol.
0
Lee SavidgeCommented:
Hey,

You learn something everyday. Been doing SQL since about 1999 and never came across the NullIf command!

Thanks for the tip :)

Best regards,

Lee < Old dog has been taught a new trick :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.