Reputation: 16015
By this I mean text records which are either:
""
)" "
)I've experimented with varying query criteria using the following table (named Table1
):
Here, the Spaces
record contains an arbitrary number number of spaces, the Empty String
record contains an empty string (""
), and the Null
record is just that.
My first thought to achieve my goal was to use a query such as:
SELECT * FROM Table1 WHERE TextField IS NULL OR Trim(TextField)=""
And this indeed returns the three target records: Spaces
, Empty String
& Null
.
However somewhat oddly, using:
SELECT * FROM Table1 WHERE TextField = ""
Returns both the Empty String
record and the Spaces
record:
Which leads me to think that my query can omit the Trim
function and become:
SELECT * FROM Table1 WHERE TextField IS NULL OR TextField=""
Alternatively I considered using:
SELECT * FROM Table1 WHERE Nz(TextField)=""
EDIT: To be specific, my question is ultimately:
What is the best way to select blank text records?
Upvotes: 7
Views: 62211
Reputation: 32642
For normal text, Access handles strings with only spaces as empty strings (e.g. SELECT " " = ""
returns -1 = True).
This means that the solution introduced by June7, WHERE TextField & "" = ""
, is likely the most efficient solution. Another contender is the Nz
function, which is handled by the database engine and somewhat optimized.
When using indexes, however, both string concatenation and function calls invalidate the index. WHERE TextField IS NULL OR TextField=""
doesn't, and will be the fastest by far if TextField
is indexed.
If you bring rich text into the mix, you're not going to get away with anything but casting it to normal text first. In Access 2016, when you enter a space in a rich text field, it actually contains the following: <div> </div>
(you can see this by using RichTextField & ""
).
For a rich text field, indexes are not going to work anyway, so you can use the following:
WHERE PlainText(RichTextField) & "" = ""
Upvotes: 8
Reputation: 538
Nz(TextField)=""
Is the approach most often used when dealing with nulls and empty strings.
In Access, The Long Text data type will automatically be trimmed to save space, this is why Trim(TextField) is the same as TextField. If you ever convert it to a Rich Text data type, these will be different. In that case:
TRIM(Nz(TextField))=""
Should cover all your bases.
Upvotes: 2