How to extract data of last week from Firebird database? - sql

I am having trouble of getting the query working, the database is a Firebird database, and the tool I use for querying the data is DBeaver. - work requirement, no other choice.
The goal is run the query and only pull the data of last week, here is one of the queries I have tried:
select *
from table 1
where 1.payment_rundate > dateadd(DAY, -7,CURRENT_TIMESTAMP())
I find that dateadd syntax is not recognized in DBeaver.

The problem isn't DATEADD, the problem is that the Firebird syntax doesn't allow CURRENT_TIMESTAMP(), it only allows CURRENT_TIMESTAMP (no parentheses) or CURRENT_TIMESTAMP(precision) where precision is 0 - 3.
In other words, use the condition:
payment_rundate > dateadd(DAY, -7, CURRENT_TIMESTAMP)
Part of the problem is that DBeaver's autocompletion generates CURRENT_TIMESTAMP() and positions the cursor inside the parentheses, expecting you to enter the desired precision. If you don't want to specify precision, it is up to you to delete those parentheses.

I would try with interval
select *
from table 1
where 1.payment_rundate between (current_date - INTERVAL 7 DAY) and current_date

I think just the below will do the trick :
select *
from table 1
where 1.payment_rundate >= current_date - 6
if payment_rundate is a timestamp then you can go with that :
select *
from table 1
where cast(1.payment_rundate as date) >= current_date - 6

Related

H2 Get Date 1 day old from current time

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

PostgreSQL: SELECT * from table WHERE timestamp IS WITHIN THIS MONTH

For some reason I'm kind of lost on how to archive:
SELECT * FROM table WHERE timestamp IS WITHIN THIS MONTH;
I've looked at https://www.postgresql.org/docs/9.4/static/functions-datetime.html, but are only able to select X days backwards.
I'm running PostgreSQL 9.4
... WHERE date_trunc('month', timestamp)
= date_trunc('month', current_timestamp);
Alternatively:
... WHERE timestamp >= date_trunc('month', current_timestamp)
AND timestamp < date_trunc('month', current_timestamp) + INTERVAL '1 month';
The second version can use an index on timestamp, the first would need one on the expression date_trunc('month', timestamp).
Why don't you just filter the month with between ?
Pass the start of this month as variable1, and the end of this month as variable2...
SELECT * FROM table WHERE
timestamp >= __month_start AND timestamp < __next_month_start
e.g.
SELECT * FROM table
WHERE
(
timestamp >= '20170701'::timestamp
AND
timestamp < '20170801'::timestamp
)
Unlike using functions in the where-clause, this maintains sargability.
What Laurenz Albe suggested will work, however you're going to have a performance penalty because you'll lose cardinality on that field, you either have to index expression you're going to query (Apparently PostgreSQL allows to do that: https://www.postgresql.org/docs/current/static/indexes-expressional.html) or create a separate column to store yyyy-mm values and query it.

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)

Convert SQL Server code to Oracle please?

Having a bit of a issue with a SQL conversion from SQL Server to Oracle.
We are passing in a datetime value (in this example just 1900-01-01) and we need to select all rows that have a modified date greater than 2 days before the date passsed in. Here is the SQL syntax that works:
SELECT *
FROM TABLENAME
WHERE CAST(LAST_MODIFIED AS DATE) > CAST(DATEADD(dd, -2, '1990-01-01') AS DATE);
Where LAST_MODIFIED is a column, and the '1900-01-01' is a value being injected to the SQL String in C# prior to it being executed. Before being asked, we prefer not to subtract the 2 days from the date before passing it to the SQL :)
So what we need then is just the above query converted to Oracle syntax... We have tried a couple things and it fails :(
Thanks,
Dave
You can write this in Oracle as:
SELECT *
FROM TABLENAME
WHERE TRUNC(LAST_MODIFIED) > (DATE '1990-01-01') - 2
Notes:
In Oracle, DATE includes a time component, so casting to a date does nothing.
Oracle supports various ways to include a date/time constant. I prefer the keyword DATE with the ISO/ANSI standard date format YYYY-MM-DD.
The - 2 is perhaps more accurately written as - interval '2' day. However, the interval notation is new(ish) to Oracle.
And, it is better in either database to write this without modifying last_modified_date:
SELECT *
FROM TABLENAME
WHERE LAST_MODIFIED >= (DATE '1990-01-01') - 1
Removing the time component is not needed, with the right date comparison.
SELECT *
FROM TABLENAME
WHERE CAST(LAST_MODIFIED AS DATE) > TO_DATE('1990-01-01','YYYY-MM-DD')-2;
SELECT *
FROM TABLENAME
WHERE TRUNC( LAST_MODIFIED ) > TO_DATE( '1990-01-01', 'YYYY-MM-DD' ) - 2;
or, if you pass the value in using the :date_value bind variable:
SELECT *
FROM TABLENAME
WHERE TRUNC( LAST_MODIFIED ) > :date_value - 2;
If the value you are passing in to the query does not have a time component then you can just do:
SELECT *
FROM TABLENAME
WHERE LAST_MODIFIED >= :date_value - 1;

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