How to replace blank (null ) values with 0 for all records?
Asked Answered
U

11

18

MS Access: How to replace blank (null ) values with 0 for all records?

I guess it has to be done using SQL. I can use Find and Replace to replace 0 with blank, but not the other way around (won't "find" a blank, even if I enter [Ctrl-Spacebar] which inserts a space.

So I guess I need to do SQL where I find null values for MyField, then replace all of them with 0.

Ungainly answered 29/12, 2009 at 21:44 Comment(0)
B
22

Go to the query designer window, switch to SQL mode, and try this:

Update Table Set MyField = 0
Where MyField Is Null; 
Bandur answered 29/12, 2009 at 21:46 Comment(1)
The first answer is also correct and would have enabled me to get it, but this one has the added benefit of Access-specific instructions that made it go a little faster. THANKS to all of you for your help, and Happy New Year!Ungainly
N
13

If you're trying to do this with a query, then here is your answer:

SELECT ISNULL([field], 0) FROM [table]

Edit

ISNULL function was used incorrectly - this modified version uses IIF

SELECT IIF(ISNULL([field]), 0, [field]) FROM [table]

If you want to replace the actual values in the table, then you'll need to do it this way:

UPDATE [table] SET [FIELD] = 0 WHERE [FIELD] IS NULL
Nahshu answered 29/12, 2009 at 21:47 Comment(2)
ISNULL in the presented form does not exist in MS Access SQLGere
@Gere Correct. The 2nd line, using the WHERE [FIELD] IS NULL is the correct usage in MS Access SQL, whether selecting or updating.Tahiti
S
5

without 'where's and 'if's ...

Update Table Set MyField = Nz(MyField,0)
Sandrocottus answered 27/9, 2013 at 21:36 Comment(0)
H
3
UPDATE table SET column=0 WHERE column IS NULL
Hexyl answered 29/12, 2009 at 21:47 Comment(0)
A
3
UPDATE YourTable SET MyField = 0 WHERE MyField IS NULL

works in most SQL dialects. I don't use Access, but that should get you started.

Austriahungary answered 29/12, 2009 at 21:47 Comment(1)
Duplicate answer... but I saw it came in at the exact same date/time as mopoke's....Tahiti
D
3

Better solution is to use NZ (null to zero) function during generating table => NZ([ColumnName]) It comes 0 where is "null" in ColumnName.

Doggish answered 4/4, 2014 at 7:57 Comment(1)
+1 !your answer solves the problem for queries not tables. Additionally, to "clean" a table, you need to create an update query and set the column values like MyField: NZ(MyField,0) RUN! Works for Access2013Daciadacie
S
2

The following Query also works and you won't need an update query if that's what you'd prefer:

IIF(Column Is Null,0,Column)
Salman answered 7/2, 2012 at 21:9 Comment(0)
N
1

Using find and replace will work if you type "null" in the find and put a zero in the replace...you will be warned that this cannot be undone.

Narbonne answered 16/7, 2016 at 3:39 Comment(0)
S
0

I would change the SQL statement above to be more generic. Using wildcards is never a bad idea when it comes to mass population to avoid nulls.

Try this:

Update Table Set * = 0 Where * Is Null; 
Soldo answered 23/1, 2011 at 11:4 Comment(0)
A
0

I just had this same problem, and I ended up finding the simplest solution which works for my needs. In the table properties, I set the default value to 0 for the fields that I don't want to show nulls. Super easy.

Archenemy answered 5/5, 2012 at 3:14 Comment(1)
But that will not handle the existing values. It only applies to values that will be added after the change.Scrophulariaceous
S
0

I used a two step process to change rows with "blank" values to "Null" values as place holders.

UPDATE [TableName] SET [TableName].[ColumnName] = "0"
WHERE ((([TableName].[ColumnName])=""));

UPDATE [TableName] SET [TableName].[ColumnName] = "Null"
WHERE ((([TableName].[ColumnName])="0"));
Sihunn answered 27/9, 2013 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.