1

My table has a category 'timestamp' where the timestamps are formatted 2015-06-22 18:59:59

However, using DBVisualizer Free 9.2.8 and Vertica, when I try to pull up rows by timestamp with a

SELECT * FROM table WHERE timestamp = '2015-06-22 18:59:59';

(directly copy-pasting the stamp), nothing comes up. Why is this happening and is there a way around it?

Mina Han
  • 671
  • 2
  • 10
  • 17
  • 1
    What DBMS are you using? – Norbert Jul 10 '15 at 21:02
  • @NorbertvanNobelen the question is tagged Vertica. – Martin Smith Jul 10 '15 at 21:13
  • 1
    From the docs Vertica time stamp has [a resolution of 1 microsecond](https://my.vertica.com/docs/5.0/HTML/Master/datatype-datetime.html) Is there in fact a decimal part stored that is not shown in your display tool? – Martin Smith Jul 10 '15 at 21:15
  • @MinaHan What is the data type of the `timestamp` column? – Kermit Jul 10 '15 at 21:16
  • 1
    @MartinSmith FYI… Regarding [that link to Vertica 5.0 Timestamp doc](https://my.vertica.com/docs/5.0/HTML/Master/datatype-datetime.html), that data type seems to have changed from that version 5 to [current version 7.1](http://my.vertica.com/docs/7.1.x/HTML/index.htm#Authoring/SQLReferenceManual/DataTypes/Date-Time/TIMESTAMP.htm?Highlight=timestamp). Previous domain ranged from 4713 BC to 5874897 AD, now 290279 BC to 294277 AD. Same resolution of "1 µs" (or [one microsecond](https://en.wikipedia.org/wiki/Microsecond), for us less mathematically educated). – Basil Bourque Jul 11 '15 at 22:30

1 Answers1

2

FYI, saying "the timestamps are formatted 2015-06-22 18:59:59" is incorrect if you are indeed using a TIMESTAMP type. Such types have their own internal representation of a date-time value, almost always a count since epoch. In your case with Vertica, 8 bytes are used for such storage. The formatting of the date-time value happens when a string representation is generated. Never confuse the string representation with the date-time value. Conflating the two may well be related to your problem/confusion.

A few different thoughts about possible problems…

String Literals

Are you sure Vertica takes strings as timestamp literals? That format you used is common SQL format. But given that Vertica seems to be a specialized database, I would double-check that.

If strings are not allowed, you may need to call some kind of function to transform the string into a date-time values.

Fractional Second

As the comment by Martin Smith points out, the doc for Timestamp-related data types in Vertica 7.1 says those types can have a fractional second to resolution of microseconds. That means up to 6 decimal places of a fraction.

So if you are searching for "2015-06-22 18:59:59" but the stored value is "2015-06-22 18:59:59.012345", no match on the query.

Half-Open

The fractional seconds issue described above is often the cause of problems people have when handling a span of time. If you naïvely try to pinpoint the ending time, you are likely to have problems. Seeing the "59:59" in your example string makes me think this applies to you.

The better approach to spans of time is "Half-Open" (or Half-Closed, whatever) where the beginning is inclusive while the ending is exclusive. Common notation for this is [). In comparison logic this means: value >= start AND value < stop. Notice the lack of EQUALS SIGN in the stop comparison. In English we would say "look for an hour's worth of invoices starting at 2:00 PM and going up to, but not including, 3:00 PM".

Half-Open for a week means Monday-Monday, for a month the first of one month to the first of the next month, and for a year the January 1 of one year to January 1 of the following year.

Half-Open means not using BETWEEN in SQL. SQL's BETWEEN has often be criticized. Instead do something like the following to look for an hour's worth of invoices. Notice the Z on the end of string literal which means "UTC time zone" ("Z" for "Zulu"). (But verify, as my SQL syntax may need fixing.)

SELECT * 
FROM some_table_
WHERE invoice_received_ >= '2015-06-22 18:00:00Z'
AND invoice_received_ < '2015-06-22 19:00:00Z'
;

This query will catch any values such as '2015-06-22 18:59:59.654321" which seems to be eluding you.

Reserved Word

I hope you have not really named your table 'table' and your column 'timestamp'. Such use of keywords and reserved words can cause explicit errors or more subtle weird problems.

Tip: The easy way to avoid any of the over a thousand reserved words in various databases is to append a trailing underscore. The SQL standard explicitly promises to never using a trailing underscore in its reserved words. So use "timestamp_" rather than "timestamp". Another example: "invoice_" table and "received_" column. I recommend doing that as a habit on everything your name in SQL: columns, tables, constraints, indexes, and so on.

Time Zone

You are using the TIMESTAMP which is short for TIMESTAMP WITHOUT TIME ZONE. Or so I presume; the Vertica doc is vague but that is the common usage as seen in the Postgres doc, and may even be standard SQL.

Anyways, TIMESTAMP WITHOUT TIME ZONE is usually the wrong type for most business purposes. The WITH time zone is misnamed and often misunderstood as a consequence: It means "with respect for time zone" where data inputs that include an offset or other time zone information from UTC are adjusted to UTC during the INSERT/UPDATE operations. The WITHOUT type simply ignores any such offset or time zone information.

The WITHOUT type should only be used for the concept of a date-time generally without being tied to any one locality. For example, saying "Christmas this year starts at beginning of December 25, 2015". That means in any time zone rather than a specific time zone. Obviously Christmas starts earlier in Paris, for example, than in Montréal.

If you are timestamping legal documents such as invoices, or booking appointments with people across time zones, or scheduling shipments in various localities, you should be using WITH time zone type.

So back to your possible problem: Test how Vertica or your client app or your database driver is handling your input string. It may be adjusting time zones as part of the parsing of the string using your client machine’s current default time zone. When sent to the database, that value will not match the stored value if during storage no adjustment to UTC was made.

Tip: Generally best practice is to do all your storage and business logic in UTC, adjusting to local time zones only where expected by user.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154