Date math with Django SQL Explorer - sql

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);

Related

timestamp string to timestamp in sql

Date data saved from stripe start_date as string timestamp like "1652789095".
Now I want to filter with this timestamp string form last 12 months.
what should I do ?
how can I filter with this timestamp string?
These are some examples - I'm sure there are plenty of options that would work.
convert to date
select *
from Table
where
to_timestamp(cast(start_date as int)::date > date_add(now(), interval -1 year);
work with unix timestamps
-- approx 1 year ago, by way of example
select *
from Table
where
start_date > '1621253095';
-- exactly one year ago, calculated dynamically
select *
from Table
where
start_date >
cast(unix_timestamp(date_add(now(), interval -1 year)) as varchar);
I'm not a MySQL guy really so forgive any syntax errors and fix up the sql as needed to work in MySQL.
Resources:
PostgreSQL: how to convert from Unix epoch to date?
https://www.postgresonline.com/article_pfriendly/3.html

Relative Dates in query

I have this query for elasticsearch SQL. It seems to be standard SQL. Here is the code:
SELECT * FROM index WHERE date > '00:00 2019-07-31'
Instead of that, I want it to be relative, so the query can be run every day and show the last 30 days. Is this possible?
I think you should be able to use current_date and interval to accomplish what you want.
SELECT * FROM index WHERE date > CURRENT_DATE - INTERVAL '30' DAY
See: https://www.elastic.co/guide/en/elasticsearch/reference/6.7/sql-functions-datetime.html

DB2 to Oracle Conversion For Basic Date Time Column Between Clause

What this is doing is selecting all columns from TABLE where a specific date time column is between last Sunday and this coming Saturday, 7 days total (no matter what day of the week you are running the query on)
I would like to have help converting the below statement into Oracle since I found out that it will not work on Oracle.
SELECT *
FROM TABLE
WHERE DATE_TIME_COLUMN
BETWEEN
current date - ((dayofweek(current date))-1) DAYS
AND
current date + (7-(dayofweek(current date))) DAYS
After poking around a bit more I was able to find something that worked for my specific problem with no administrator restrictions for whatever reason:
SELECT *
FROM TABLE
WHERE DATE_TIME_COLUMN
BETWEEN
TIMESTAMPADD(SQL_TSI_DAY, DayOfWeek(Current_Date)*(-1) + 1, Current_Date)
AND
TIMESTAMPADD(SQL_TSI_DAY, 7 - DayOfWeek(Current_Date), Current_Date)
Use TRUNC() to truncate to the start of the week:
SELECT *
FROM TABLE
WHERE DATE_TIME_COLUMN
BETWEEN trunc(sysdate, 'WW')
and
trunc(sysdate + 7, 'WW');
sysdate is the current system date, trunc truncates a data, and WW tells it to truncate to the week (rather than day, year, etc.).
Assuming DATE_TIME_COLUMN is - as it should be - of datatype date, I think this gets what you want.
where DATE_TIME_COLUMN between
next_day(sysdate,'SUNDAY')-7 and next_day(sysdate,'SATURDAY')
You may need to tweak it a bit. Please follow up studying the official docs on the NEXT_DAY function in the relevant docs at http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions106.htm#SQLRF00672
The proposed TRUNC does not guarantee you get the date of a particular day of the week:
SQL> alter session set nls_date_format='day dd-mon-yyyy';
Session altered.
SQL> select trunc(sysdate,'WW') from dual;
TRUNC(SYSDATE,'WW')
---------------------
friday 22-jan-2016
SQL> select trunc(sysdate+7,'WW') from dual;
TRUNC(SYSDATE+7,'WW')
---------------------
friday 29-jan-2016

In MonetDB, how can I get the date as an integer?

I want to be able to do something like
SELECT cast(my_date_col AS int) FROM my_table;
I would like to get the integer which MonetDB uses internally, i.e. the value you'd find if you looked into the BAT structure and got the appropriate element in code in MonetDB's GDK. Now, AFAICT, this internal value is the number of days since the Epoch, being Jan 1st on "Year 0" (so January 3rdt year 2 would be 366+365+2 = 732).
The best I could actually manage is
SELECT my_date_col AS int - cast('1-1-1' AS date) - 366 FROM my_table;
As MonetDB won't accept "Year zero" dates. This is rather an ugly hack, I'd like to do better. Help me?
If you're trying to get the number of days between "my_date_col" and 1970-01-01, in standard SQL you'd just subtract the one from the other. Your platform, monetdb, seems to support this syntax, but I don't have it installed. I wrote these examples in PostgreSQL.
select current_date - date '1970-01-01' as num_days;
num_days
--
16213
Check that result by adding 16213 days to the current date (2014-05-23).
select cast ((date '1970-01-01' + interval '16213' day) as date) as target_date
target_date
--
2014-05-23
The cast is necessary, because the result of this addition is a timestamp, not a date.
In your case, you want a column name instead of "current_date". So you're looking for something along these lines.
select my_date_col - date '1970-01-01' as num_days
from your-table-name;

current_date usage

I would like to subtract the current date from a given date in SQL or in JDBC. I would like to have the result in hours. Not sure how to treat the date in that case. Can some one give me a basic example Please.
You don't mention which database server you use - here's a sample in MySQL.
select hour(TIMEDIFF('2011-03-15 19:59:59.000001', now()))
Note: the "hour" function doesn't deal with rounding, so if you need that, you need to do some further arithmetic.
Date functions are pretty vendor-specific, so your mileage may vary....
In standard SQL
select (date '2011-03-16' - CURRENT_DATE) as days_different,
(date '2011-03-16' - CURRENT_DATE) * 24 as hours_different
days_different hours_different
--
1 24
As I write this, CURRENT_DATE = '2011-03-15'.