Select record based on date with MS Access - java

Initially I have a specific column to store age in my Table and I use the following to perform the select query
select count(*) from DonorDetails where age < 25
Now I have removed the age column and used dob column (with Date/Time datatype) instead. How should I write the count query which gives the same result as before ?

In the Immediate window, you can use DateSerial with a mix of other date functions to give you the date 25 years ago from today.
? DateSerial(Year(Date()) - 25, Month(Date()), Day(Date()))
2/22/1989
So in your query maybe you want something like ...
WHERE [DOB] > DateSerial(Year(Date()) - 25, Month(Date()), Day(Date()))

Here is a sample of how your SQL should look:
SELECT count(*)
FROM DonorDetails
WHERE (((DateDiff("yyyy",[dob],Date()))>25));

Related

localdate value don't return result [duplicate]

This question already has answers here:
How to persist LocalDate with JPA?
(5 answers)
Closed 2 years ago.
I use Java 11, spring boot 2.3.4 with jpa (hibernate implementation)
I have an entity
public class Prba{
Long prId;
Long baId;
LocalDate startDate;
}
My jpql query
select
prba from Prba prba
where
prba.prId != :prId
and
prba.baId = :baId
and
prba.startDate = :date
baId, prId are long (number(18,0) in oracle)
date is localdate (date in oracle)
query generated
select * from prba
where
prba.pr_id != ?
and
prba.ba_id = ?
and
prba.start_date = ?
I get no result
Without start_date condition I get a row, start_value is 15-08-2020, it's the java value i pass, but i don't get any result
Edit
if i use
to_date(prba.start_date, 'DD-MM-YYYY) = TO_DATE ('15-08-20', 'DD-MM-YYYY')
that work
Is there any method to use in jpa when I use localdate
Edit 2
create table prba (
pr_id number(18,0),
ba_id number(18,0),
startDate date
)
Issue it's in orale a time is saved with a date, if you pass by a ui tool to insert data, 00:00:00 for the time will not be used and if you compare you will get wrong date
You need to allow for the fact that in oracle a DATE data type includes the time down to the second. So if the date in your oracle table has, effectively '23-Sep-2020 15:24:32', and the date you are comparing it to is just a date with no time, then they will not match. Everything you've presented - especially the use of the to_date to correct it, suggests that is your issue. BTW,
Here's a demo of what I just said, with an example of the normal adjustment to deal with DATE and ignore the time component:
SQL> -- create and populate test table
SQL> create table my_test (dob date);
Table created.
SQL> insert into my_test values (sysdate);
1 row created.
SQL> -- unconditional select to prove what's there
SQL> select dob,
2 to_char(dob,'dd-mon-yyyy hh24:mi:ss') date_time
3 from my_test;
DOB DATE_TIME
--------- --------------------
23-SEP-20 23-sep-2020 15:07:50
1 row selected.
SQL> -- use where clause that does not account for the time component, it returns nothing
SQL> select dob,
2 to_char(dob,'dd-mon-yyyy hh24:mi:ss') date_time
3 from my_test
4 where dob = to_date('23-09-2020','dd-mm-yyyy')
5 ;
no rows selected
SQL> -- use trun() to eliminate the time component
SQL> select dob,
2 to_char(dob,'dd-mon-yyyy hh24:mi:ss') date_time
3 from my_test
4 where trunc(dob) = to_date('23-09-2020','dd-mm-yyyy')
5 ;
DOB DATE_TIME
--------- --------------------
23-SEP-20 23-sep-2020 15:07:50
1 row selected.
SQL> -- clean up the test
SQL> drop table my_test purge;
Table dropped.
SQL> spo off
Some other observations:
your use of "to_date(prba.start_date, 'DD-MM-YYYY)" ... the to_date function take a string as its input. So if prba.start_date is of data type DATE, you force oracle to first do an implied to_char to make it the string required by to_date. And it is only a matter of time before that comes back to bite you, due to conflicting NLS_ settings. And if prba.start_date is NOT of data type DATE, then that is in itself a design failure.
I'd suggest you give some thought to your naming conventions.
select
prba from Prba prba
you have a table name PRBA, in that table you have a column also named PRBA. Not a good idea. Spend a little time researching column and table naming conventions.

Selecting the records with maximum date value

String sql1="SELECT MAX(date),epf_rte_emp,epf_rte_com,etf_rte FROM Fixed_Rates1";
I'm using this query to select the record with the maximum date value, but it gives the latest updated record. I want the record with the maximum date.This query gives the record with maximum date in the SQLiteManager, but doesn't gives the required output in netbeans. Could someone please help me in this?
If you want one record, then something like this shoudl work:
select fr.*
from fixed_rates1 fr
order by date desc
limit 1;
Note: I am guessing you are using MySQL, because your query would fail in most other databases. The method for limiting results depends on the database.
If you want all rows with the maximum date:
select fr.*
from fixed_rates1 fr
where fr.date = (select max(fr2.date) from fixed_rates1 fr2);

Get data for specific month from MS Access 2016 db

Good day, I am struggling with an sql select query from my java application. My application connects with an MS Access database. I would like to retrieve all records logged during a specific month. This is my statement:
Select * from tbl q
Join (
Select s.Customer from tbl s
Where Month(s.LogDate) = 1 And Year(s.LogDate) = 2017);
Problem is that my resultset returns data logged for both January 2017 and December 2016. I have tried different approaches one of which was to pass an sql date(#date#) but I still get the same result. What am I doing wrong?
You shouldn't need the join or the subquery. Try
Select * from tbl where month(logdate)=1 and year(logdate)=2017

Delete item from database after X days

I am trying to make a Java Thread, this thread have to delete from an MySQL database all the records, older than 7 days.
In my table i have a column that contain the date like this: 2013-10-28 17:00:00 .
And to do this i want to use JDBI library. and my question here if any one could give me and example of the query that i have to write.
I think it should look like this:
h.execute("Delete from MyTable where date >= (dt.now.dayofmonth() -7)
h.execute("DELETE FROM MyTable WHERE NOW() >= ADDDATE(date, INTERVAL 7 DAY);");
Try this::
h.execute("Delete from MyTable where DATEDIFF(CURDATE(), dateCOLUMN)>7");
"delete from MyTable where date >= date_sub(now(), interval 7 day)"

How do I get query for products added in last 30 days (HSQLDB)

Before anyone comments, I must use HSQLDB RDBMS; it's for university java project. My question is how would I get list of invoices created in last 30 days
CREATE TABLE Invoices(
rid INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
invoicedate TIMESTAMP NOT NULL);
You could use HSQL's dateadd function:
select *
from Invoices
where invoicedate > dateadd('day', -30, CURRENT_DATE)

Categories

Resources