Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add days to timestamp and get a timestamp back

In Spark SQL I could not find a function that adds days to a timestamp and receive a timestamp back, and I can use a calculated value to do so.

This works, but static

SELECT col1 + interval 2 days FROM 

VALUES make_timestamp(2021, 12, 28, 6, 30, 45.887)

I need one that I can calculate. This doesn't work (I cannot fill it with column value either):

SELECT col1 + interval (5-3) days 

FROM VALUES make_timestamp(2021, 12, 28, 6, 30, 45.887)

This is not good either (as it gives a date back):

SELECT date_add(col1,1) FROM 

VALUES make_timestamp(2021, 12, 28, 6, 30, 45.887)
like image 506
Kozmo Avatar asked Dec 13 '25 09:12

Kozmo


2 Answers

One way is to convert timestamp to unix time and add desired number of seconds.

scala> spark.sql("select from_unixtime(unix_timestamp('2021-10-15 10:11:12') + 10 * 24*60*60) plus10days").show(false)
+-------------------+
|plus10days         |
+-------------------+
|2021-10-25 10:11:12|
+-------------------+
like image 118
mazaneicha Avatar answered Dec 16 '25 15:12

mazaneicha


Instead of using directly interval constructor, you can build a string representing your interval with concat SQL function and then cast it to an interval with cast SQL function:

SELECT col1 + cast(concat(5-3, ' days') as interval)

FROM VALUES make_timestamp(2021, 12, 28, 6, 30, 45.887)

which returns the following result:

+----------------------------------------------------------------------------------+
|CAST(col1 + CAST(concat(CAST((5 - 3) AS STRING),  days) AS INTERVAL) AS TIMESTAMP)|
+----------------------------------------------------------------------------------+
|2021-12-30 06:30:45.887                                                           |
+----------------------------------------------------------------------------------+
like image 37
Vincent Doba Avatar answered Dec 16 '25 16:12

Vincent Doba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!