Link to home
Start Free TrialLog in
Avatar of jimshock
jimshock

asked on

Need to update blank fields to null

Basically I need the opposite of the solution in this post:
https://www.experts-exchange.com/questions/22129219/Replace-null-with-blank.html?sfQueryTermInfo=1+blank+field+null+updat
I need to write a SQL statement that will update all blank fields in all columns of a table to Null, and in fields that are Not Null eliminate anything more than a single blank space (this will account for space between a name, ie, "Microsoft Office") -- can anyone help?
Avatar of jimshock
jimshock

ASKER

I mean "...in fields that are Not Null eliminate any ADD'L SPACES more than a single blank space" -- obviously want to leave text data in the Not Null fields :-)
Avatar of Jim Horn
Make a backup first, then give this a whirl...

UPDATE YourTableName
SET YourColumnName = NULL
WHERE Len(LTRIM(RTRIM(COALESCE(YourColumnName, '')))) = 0
SOLUTION
Avatar of NeoTeq
NeoTeq

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I apologize for not getting back sooner. My question on these is, since I want the statement to go across all fields in the table, what do I set YourColumn to?  Or do I have to do each column one at a time?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Understood.  The "blanks to Null" part of your SQL works, but I've still got blanks spaces after fields with text in them...
No biggie, I'm just going to use NeoTeq's second statement for the blank spaces after fields and yours for the blanks to Null.  I really appreciate both you guys help!!
Thanks for the split.  Good luck with your project.  -Jim