SAP HANA: how to get the list of dates from system using SQL hana where the date range is from '2019-10-24' and CURRENT_DATE - hana

how to get the list of dates from system using SQL hana where the date range is from '2019-10-24' and CURRENT_DATE

One way to achieve this is the SERIES_GENERATE_DATE table function:
SELECT *
FROM SERIES_GENERATE_DATE('INTERVAL 1 DAY', '2019-10-24', CURRENT_DATE)

Related

Date math with Django SQL Explorer

I have implemented Django SQL Explorer on a project and am attempting to build a query that will pull entries between today's date and 12 months prior to today's date, but I'm having a difficult time figuring out how SQL Explorer does date math. So far, about the only thing I've been able to discover is that SQL Explorer uses current_date for today's date.
So:
SELECT current_date;
returns:
2021-10-02
I've tried using the MySQL implementation (since that's the database my project is using):
SELECT current_date - INTERVAL 12 MONTH;
returns:
near "12": syntax error
About the closest I've been able to come is some very simple math that just work on the year and strips all the rest of the date information away:
SELECT current_date - 1;
returns:
2020
Can anyone please help me figure out how to return the date 12 months prior to today's date in Django SQL Explorer?
SELECT current_date
SELECT current_date - [12 MONTH];
should return:
2021-10-02
2020-10-02
Thanks in advance!
I suspect that you are using SQLite and not MySql, in which case you want:
SELECT date(CURRENT_DATE, '-12 month')
Or:
SELECT date(CURRENT_DATE, '-1 year')
You must use DATE_SUB function in MySQL syntax. for example:
SELECT DATE_SUB('2021-10-02', INTERVAL 1 YEAR);
or
SELECT DATE_SUB('2021-10-02', INTERVAL 12 MONTH);

AWS Athena - Format and filter datetime

I have a table which is fed two different date formats:
d/m/Y & m/d/Y. The date format wanted is d/m/Y
I am able to select the date column and do a check and format if the date is in the wrong format.
This is my current SQL query:
SELECT COALESCE(TRY(date_format(date_parse(tbl.date, %d/%m/%Y), %d/%m/%Y)),
TRY(date_format(date_parse(tbl.date, %m/%d/%Y), %d/%m/%Y))) as date
FROM xxx
That fixes the mismatched dates...however I also need to query a date range e.g. the last 7 days.
If I add a WHERE statement it does not execute as I have already queried the date earlier.
How can I format my dates AND filter based on a given range (last 7 days)?
In ANSI SQL -- implemented by Presto, which Athena is based on -- the WHERE clause cannot reference the SELECT projections, so you need a aubquery:
SELECT *
FROM (
SELECT COALESCE(TRY(date_parse ....... AS date
FROM xxx
)
WHERE date > current_date - INTERVAL '7' DAY

Dynamic Date Table

I am creating a Data Model in PowerPivot and am wondering if there is anyway I can create a dynamic date table in SQL. I was able to create one in PowerQuery however there are some bugs in PowerQuery(read only connection) when a table is modified in PowerPivot. What I am looking for is to have a start date of 1/1/2013 (interval is days) and as each new year rolls around rows are added to the date table. Is there anyway to do this?
I am running Postgres
So far I came up with this,
SELECT * FROM dbo.fof_GetDates('1/1/2013', GETDATE())
But I want it to display all dates till end of the year.
The completely dynamic approach would be a query based on generate_series():
SELECT the_date::date
FROM generate_series('2013-01-01 0:0'::timestamp
, date_trunc('year', now()::timestamp)
+ interval '1 year - 1 day'
, interval '1 day') the_date;
Always use ISO 8601 format for dates and timestamps, which works irregardless of locale settings.
A final cast to date (the_date::date), because the function returns timestamp (when fed timestamp arguments).
The expression
date_trunc('year', now()::timestamp) + interval '1 year - 1 day'
calculates the last day of the current year. Alternatively you could use EXTRACT (year FROM now())::text || '-12-31')::date, but that's slower.
You can wrap this into a custom "table-function" (a.k.a. set-returning function) that you can basically use as drop-in replacement for a table name in queries:
CREATE OR REPLACE FUNCTION f_dates_since_2013()
RETURNS SETOF date AS
$func$
SELECT the_date::date
FROM generate_series('2013-01-01 0:0'::timestamp
, date_trunc('year', now()::timestamp)
+ interval '1 year - 1 day'
, interval '1 day') the_date;
$func$ LANGUAGE sql STABLE;
Example:
SELECT * FROM f_dates_since_2013();
Going one step further, you could create a table or - more elegantly - a MATERIALIZED VIEW based on this function (or the underlying query directly):
CREATE MATERIALIZED VIEW my_dates(the_date) AS
SELECT * FROM f_dates_since_2013();
Call:
SELECT * FROM my_dates;
All you have to do now is to schedule a yearly cron job that runs REFRESH MATERIALIZED VIEW at the start of each new year:
REFRESH MATERIALIZED VIEW my_dates;

postgres select all dates from interval

How to with Postgres I Could retrieve all dates using parameters 'from' and 'until'
example:
select date from 'maybe a system table' where date >= :from and date <= :to
then te result is for (02-01-2015 and 05-01-2015)
date
------------
02-01-2015
03-01-2015
04-01-2015
05-01-2015
What is the Best way to do this with Postgres, I know how to do with Oracle, but I need change my Database?? I Want to do a join with a table that does not have register with all dates and my report needs all days in one days interval
Regards
In Postgres, you can use generate_series(). Here is an example for the days in January, 2015:
select generate_series('2015-01-01'::timestamp, '2015-01-31'::timestamp,
'1 day')::date

How to get current date - two weeks date using Postgresql?

I am working with Postgresql database. I need to get date which is two weeks prior to current date using Postgresql database.
select date(now() - interval '2 week') from test_base
If today's date is 2014-05-08 then above sql should give me 2014-04-24?
But somehow above query is not working at all?
SELECT now() - interval '2 week';
This works just fine. What is the exact error you are getting?
Since you are not pulling any data from table test_base there is no need to include that in a FROM clause.