Search code examples
sqlvertica

How can I substract today date (or find interval id days) in a table?


I am using Vertica SQL and have the following table:

CREATE temp TABLE XXX (DATE TEXT, PRICE INTEGER);
INSERT INTO XXX VALUES 
('2019-04-27 01:00', 1), ('2019-04-27 02:30', 3), ('2019-04-27 18:00',2), 
('2019-04-28 17:00', 2), ('2019-04-28 21:00', 5), 
('2019-04-29 17:00',50), ('2019-04-29 21:00',10), 
('2019-04-30 17:00',10), ('2019-04-30 21:00',20), 
('2019-05-01 17:00',40), ('2019-05-01 21:00',10), 
('2019-05-02 17:00',10), ('2019-05-02 21:00', 6);

I want to find a difference (compute in days) between today and dates in the table. I tried to use

select (date_trunc('month',DATE)-date_trunc('month',current_date)) as DATE, PRICE from XXX

As I understood initial table has 13 rows, however date_trunc('month',current_date) has 1 row. As a result I have an error. How this issue can be solved?

And as I use Vertica should I add PostgreSQL or MySQL in tags?


Solution

  • Try with either datediff or timestampdiff

     datediff('day', date , Now()) as DaysBetweenDates
    
     timestampdiff(day, date, Now()) as DaysBetweenDates