H2 Get Date 1 day old from current time - sql

I'm currently working on a really fun problem. I want to get a date that is one day old (from current date) and then compare it to now.
The exact way to do this in PostreSQL is this:
select * from table WHERE date < now() - '1 day'::interval;
How do I do this in H2 JDBC? Does anybody know?
Grateful for any assistance!

Simply subtract the number of days from current_date
select *
from the_table
where the_date_column < current_date - 1;
The above would work in Postgres just as well.

You can try the DATEADD function. It works for addition and subtraction:
select * from table WHERE date < DATEADD('DAY', -1, CURRENT_DATE);

Related

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)

Filter Data for 30 months using subquery with INTERVAL function in Teradata

I would like to filter out the data using a sub query in the interval function
Following is the query i use
SEL * FROM my_table WHERE MY_DATE < CURRENT_DATE- INTERVAL '30' MONTH;
The above query works, However i want to parameterize the period '30' using a sub query. Please suggest how to achieve this.
Thanks in Advance
Don't use interval calculations with year/month as it will fail, e.g. DATE '2016-12-31' + INTERVAL '30' MONTH results in 2019-06-31 (according to Standard SQL) which obviously doesn't exist.
SELECT *
FROM my_table
WHERE MY_DATE < ADD_MONTHS(CURRENT_DATE, (SELECT -col FROM tab));
If col is actually an INTERVAL you need to cast it to an INT.

Difficulties retrieving data with SQL now() function

I am trying to retrieve some data utilising SQL's now() function. It works problem free for monthly and weekly queries. However, when I try to pull daily data, the numbers are way off. Sample code I am using:
SELECT
COUNT(distinct be.booking_id)AS "Number of Inquiries"
FROM booking_events be
WHERE be.event = 'inquire'
AND DATE_PART('day', be.created_at) = DATE_PART('day', now())
I tried figuring it out for a couple of days, but without any luck.
Hmmm, if you want today's data, I might suggest:
SELECT COUNT(distinct be.booking_id)AS "Number of Inquiries"
FROM booking_events be
WHERE be.event = 'inquire' AND
be.created_at >= current_date and
be.created_at < current_date + interval '1 day';
I don't think date_part() helps you unless you want all records from the same day of the month -- say, Jan 25, Feb 25, Mar 25, and so on.
You might be thinking of date_trunc() instead:
WHERE be.event = 'inquire' AND
date_trunc(be.created_at) = current_date
Please explain what is your problem - here's example of working query you say does not work:
EDIT:
select created_at,extract(day from created_at),DATE_PART('day', now()),extract(day from now()),now() from be where DATE_PART('day', be.created_at) = DATE_PART('day', now())
EDIT2
just in case you are trying to compare today agaist date in row, then use date_trunc, like:
select date_trunc('day',created_at), date_trunc('day',now()) from be ;

How to retrieve the records based on a date from oracle database

I have a table with date column in it. I need to fetch the records from it based on
the given date.
Currently when i used the query:
select * from workingemployee_data where created_date like '20-Jan-2012'
I am getting those records which have created_date on 20-Jan-2012
But i want to get the records those were created 10 days earlier to a given
date (i.e) 20-Jan-2012.
Please suggest me on this.
This gives all records between today and 10 days ago:
SELECT *
FROM workingemployee
WHERE created_date BETWEEN sysdate - INTERVAL '10' DAY
AND sysdate
This gives all records entered exactly 10 days ago:
SELECT *
FROM workingemployee
WHERE created_date = sysdate - INTERVAL '10' DAY
Replace sysdate with exact date if you want.
Why do you use like and not = ?
Assuming that created_date is of type DATE, it's bad practice to rely on implicit conversion according to NLS_DATE_FORMAT (this is what happens when you compare a date and a string)
dd-mon-yyyy isn't a good format for querying since it deffers according to NLS_LANGUAGE better use mm for months numbers
So, either use #mvp's answer or do something like this:
SELECT *
FROM workingemployee
WHERE trunc(created_date) = to_date('20-01-2013', 'dd-mm-yyyy') - 10
SELECT *
FROM workingemployee
WHERE created_date > sysdate - INTERVAL '10' DAY;