Skip to content
Advertisement

How can i add days to a Hive timestamp without loosing hours, minutes and seconds

I am using Hive 2.6.5 and when i want to add days to my timestamp, it doesn’t keep the hours, minutes and seconds.

Exemple

in addition to that it returns a wrong result as :

I would like it to return the value 2021-01-17 09:34:21

Thank you

Advertisement

Answer

  1. date_add truncates
  2. Unnecessary unix_timestamp+from_unixtime conversion

Convert to timestamp, add interval:

Result:

Timestamp is displayed with zero millisecond part, it is default timestamp representation. If you want it as string without milliseconds, format using date_format or simply take substr()

Result:

And the same using substr:

If you need to calculate interval dynamically and your Hive version does not support it, see this example:

Result:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement