Get a previous date from current date in Druid SQL - sql

How to get the date 7 days before from the current date in Druid SQL? I have done similar in Postgres SQL as
CURRENT_DATE - interval '7 day'
I need to do the same in Druid SQL query

you can just use the timestampadd function like this: TIMESTAMPADD(DAY, -7, CURRENT_TIMESTAMP) and you can use it within the where clause of a select statement to display records greater than or equal to 7 days as like this:
select * from "testdatasource" WHERE "__time" >= TIMESTAMPADD(DAY, -7, CURRENT_TIMESTAMP)
In case you want to round it to midnight then you can nest it with date_trunc function as follows:
DATE_TRUNC('DAY', TIMESTAMPADD(DAY, -7, CURRENT_TIMESTAMP))

Use timestamp_expr { + | - } <interval_expr> or TIME_SHIFT(<timestamp_expr>, <period>, <step>, [<timezone>]) - see the docs at https://druid.apache.org/docs/latest/querying/sql.html#time-functions which also explain the difference between the two.

Related

Getting records between current date and next x days in Presto

Hope someone can help me out. I'm trying convert a line of SQL to work with Presto. Currently in SQL I do the following to get all records that are due in the next 0-5 days:
((EventStartDate)between getdate()-1 and dateadd(day, 5, getdate()))
I thought it would be something like this in Presto
EventStartDate between current_date and interval '5' day
But get the following error in AWS Athena: Cannot check if date is BETWEEN date and interval day to second
Thanks,
Mark
Interval needs a date or timestamp to be used and BETWEEN can only be made between to equal entities to dates twp timestamps two numbers
So do this instead
EventStartDate between current_date and current_date + interval '5' day

count the number of days of current month from day 1 until yesterday

I am trying to calculate the number of days of the current of month from day 1 until yesterday without the need of changing the count manually. The original SQL as below:
select order_id
from orders
where date > dateadd(-23 to current_date) and date < 'today'
the desired code is something like
select order_id
from orders
where date > dateadd(datediff(day,firstdayofthemonth,current_date) to current_date) and date < 'today'
Appreciate any help
In firebird you could do:
WHERE
date >= DATEADD(1 - EXTRACT(DAY FROM CURRENT_DATE) DAY TO CURRENT_DATE)
AND date < CURRENT_DATE
In addition to the answer provided by Mark, you can also use BETWEEN (starting with Firebird 2.0.4)
WHERE
date BETWEEN current_date - extract(day from current_date) + 1
AND current_date - 1
P.S. all those answers rely upon DATE data type (thus, date column and CURRENT_DATE variable) having no time part. Which is given for modern SQL dialect 3. But if Dialect 1 would get used it is not given.
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-commons-predicates.html
https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-background.html#fblangref25-structure-dialects
In addition to the answer provided by GMB, you can also use fact that Firebird allows addition of days to a date without needing to use dateadd:
date > current_date - extract(day from current_date)
and date < current_date

How to get table data between two time using sql query

i am using this below sql query to get the table data those was updating yesterday between 12:00 AM to 11:59 AM. In this query i need to put date on daily basis but i don't want to put date again and again so i want another query to get table data without updating date.
select *
from transaction_persistence
where currentdatetimestamp between '18-MAY-2017 12.00.00 AM' and '18-MAY-2017 11.59.59 AM';
Use now() or curdate():
select *
from transaction_persistence
where currentdatetimestamp >= CURDATE() and
currentdatetimestamp < CURDATE() + interval 12 hour;
Note: When working with date or date/time values, BETWEEN is dangerous. In your case, you are missing one second of every half day.
EDIT:
You get Oracle errors with Oracle, not MySQL:
select *
from transaction_persistence
where currentdatetimestamp >= trunc(sysdate) and
currentdatetimestamp < trunc(sysdate) + 0.5
Use DATE_SUB() and CURDATE()
SELECT *
FROM transaction_persistence
WHERE currentdatetimestamp<CURDATE() AND currentdatetimestamp>=DATE_SUB(CURDATE(),INTERVAL 1 DAY)

BigQuery SQL WHERE Date Between Current Date and -15 Days

I am trying to code the following condition in the WHERE clause of SQL in BigQuery, but I am having difficulty with the syntax, specifically date math:
WHERE date_column between current_date() and current_date() - 15 days
This seems easy in MySQL, but I can't get it to work with BigQuery SQL.
Use DATE_SUB
select *
from TableA
where Date_Column between DATE_SUB(current_date(), INTERVAL 15 DAY) and current_date()
Remember, between needs the oldest date first
You should probably switch the two around - the syntax should be the following:
WHERE date_column BETWEEN DATE_ADD(CURRENT_DATE(), -15, 'DAY') AND CURRENT_DATE()
This works for me.
WHERE DATE(date_column) BETWEEN DATE(DATE_ADD(CURRENT_DATE(), -15, 'DAY'))
AND CURRENT_DATE()

SQL Server query to get selected rows by adding particular no of days to specific date

i m having following type of simple sql server table
Here I want to retrieve all the rows where Notice Created On date + Visibility days are less than or equal to current datetime.
I believe what you are looking for is this, using the DATEADD function
SELECT *
FROM TABLE
WHERE GETDATE() <= DATEADD(dd, NoticeVisibilityDays, CreatedOn)
Assuming a visibility of 9 days, your where clause should read
where (created + interval 9 day) <= now()
CURRENT_TIMESTAMP or now(), etc.
Where are you getting visibility from?
UPDATE:
then use
where (getDate() + interval visibility day) <= now()
You can also use
where DATE_ADD(getDate(), interval visibility day) <= now()