66

I have a query that looks like the following:

SELECT * FROM table WHERE timestamp = NULL;

The timestamp column is a timestamp with time zone data type (second type in this table). This is in PostgreSQL 8.4.

What I'm trying to accomplish is to only select rows that have not had a timestamp inserted. When I look at the data in pgAdmin the field is empty and shows no value. I've tried where timestamp = NULL, 'EPOCH' (which you would think would be the default value), a valid timestamp of zeros (0000-00-00 00:00:00-00, which results in a out of range error), the lowest date possible according to the docs (January 1, 4713 BC) and a blank string ('', which just gets a data type mismatch error). There also appears to be no is_timestamp() function that I can use to check if the result is not a valid timestamp.

So, the question is, what value is in that empty field that I can check for?

Thanks.

EDIT: The field does not have a default value.

1 Answer 1

117

null in SQL means 'unknown'.

This means that the result of using any comparison operator, like =, with a null is also 'unknown'.

To check if a column is NULL (or not NULL), use the special syntax of IS NULL (or IS NOT NULL) instead of using =.

Applying that to your statement,

SELECT * FROM table WHERE timestamp IS NULL;

should work.

Sign up to request clarification or add additional context in comments.

3 Comments

I think he wants where timestamp IS NULL (no not) but yeah, nothing can equal null since null is unknown.
No, that would just select all timestamps. However you did inadvertently give me the answer: you need to use timestamp IS NULL, not timestamp = NULL, so thanks.
This doesn't retrieve the record for me if the timestamp is blank.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.