Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent dates with uncertainty in PostgreSQL

PostgreSQL provides the date format datatype to store dates. The problem with these dates is however they can't - as far as I know - reason about uncertainty.

Sometimes one does not know the full date of something, but knows it happened in January 1995 or in "1999 or 2000" (date2). There can be several reasons for that:

  • People don't remember the exact date;
  • The exact date is fundamentally unknown: for instance a person was last seen on some day and found death a few days later; or
  • We deal with future events so there is still some chance something goes wrong.

I was wondering if there is a datatype to store such "dates" and how they are handed. It would result in thee-valued logic for some operations like for instance date2 < 20001/01/01 should be true, date2 < 2000/01/01 be possible and date2 < 1998/01/01 should be false.

If no such datatype is available, what are good practices to construct such "table" onself?

like image 258
Willem Van Onsem Avatar asked Dec 31 '14 22:12

Willem Van Onsem


People also ask

How do I change the date format in PostgreSQL?

Use dd/mm/yyyy for numeric date representations. Use mm/dd/yyyy for numeric date representations. A value for SET DATESTYLE can be one from the first list (output styles), or one from the second list (substyles), or one from each separated by a comma.

What is the date format in PostgreSQL?

What is the date format in PostgreSQL? The date format for the date data type in PostgreSQL is yyyy-mm-dd . This is the format used for both storing data and for inserting data.

How do dates work in PostgreSQL?

DATE data type in PostgreSQL is used to store dates in the YYYY-MM-DD format (e.g. 2022-03-24). It needs 4 bytes to store a date value in a column. Note that the earliest possible date is 4713 BC and the latest possible date is 5874897 AD. It is highly important to retain code readability at the stage of writing it.

How do I get the difference between two dates in PostgreSQL?

To calculate the difference between the timestamps in PostgreSQL, simply subtract the start timestamp from the end timestamp. Here, it would be arrival - departure . The difference will be of the type interval , which means you'll see it in days, hours, minutes, and seconds.


2 Answers

There are several different ways to approach fuzzy dates. In PostgreSQL, you can use

  • a pair of date columns (earliest_possible_date, latest_possible_date),
  • a date column and a precision column ('2012-01-01', 'year'), or
  • a range data type (daterange), or
  • a varchar ('2013-01-2?', '2013-??-05'), or
  • another table or tables with any of those data types.

The range data type is peculiar to recent versions of PostgreSQL. You can use the others in any SQL dbms.

The kind of fuzziness you need is application-dependent. How you query fuzzy dates depends on which data type or structure you pick. You need a firm grasp on what kinds of fuzziness you need to store, and on the kind of questions your users need answered. And you need to test to make sure your database can answer their questions.

For example, in legal systems dates might be remembered poorly or defaced. Someone might say "It was some Thursday in January 2014. I know it was a Thursday, because it was trash pick-up day", or "It was the first week in either June or July last year". To record that kind of fuzziness, you need another table.

Or a postmark might be marred so that you can read only "14, 2014". You know it was postmarked on the 14th, but you don't know which month. Again, you need another table.

Some (all?) of these won't give you three-valued logic unless you jump through some hoops. ("Possible" isn't a valid Boolean value.)

like image 179
Mike Sherrill 'Cat Recall' Avatar answered Nov 09 '22 21:11

Mike Sherrill 'Cat Recall'


To add to what Mike posted I would use date comments such as:

date            Comment
-------------------------------------------------------------------
1/1/2010        Sometime in 2010
7/8/2014        Customer says they will pay the second week in July
1/1/2015        Package will arrive sometime next year in January

Also, you can use date parts. Create a separate column for the Year, Month, and Day. What ever in unknown leave it blank.

like image 28
Luke101 Avatar answered Nov 09 '22 22:11

Luke101