Get data for specific month from MS Access 2016 db - java

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

Related

I want to fetch 30 days back results from sqlite DB

I am trying to fetch 30 days back results from today in order to see who have not paid last month.
I tried something
SELECT * FROM MemberInformation WHERE DateOfAd = 'DATEADD(Day,-30,getdate())'
and query successfully executed but did not fetch the values I wanted.
My database:
please guys help me out...
DATEADD is not work in sqlite you but you can use its sqlLite equivalent DateTime like code below :
select * from TableName where DateColumn < DateTime('Now', 'LocalTime', '-10 Day');
http://www.sqlite.org/lang_datefunc.html
SQLite equivalent of SQL Server DateAdd function
Try
SELECT * FROM MemberInformation WHERE DateOfAd BETWEEN datetime('now', '-30 days') AND datetime('now', 'localtime');
or Try to change where condition as,
WHERE DateOfAd BETWEEN datetime('now', 'localtime', '-30 days') AND datetime('now', 'localtime');

How to use date_format when using JPQL/JPA

I am doing Java EE with MySQL as database and implementing JPA on my codes.
I have no problem retrieving the data from MySQL Workbench but when I change my syntax to JPQL's it does not work.
For e.g. in MySQL - it works
SELECT date_format(s.date,'%Y, %m, %d') from Transactions s;
in JPQL - it does not
SELECT date_format(s.date,'%Y, %m, %d') from TransactionEntity s;
How do i modify to suit the JPA query?
Note:
in JPQL the following works
SELECT s.date from TransactionEntity s;
SQL function date_format is not part of JPQL, as any documentation would tell you, so don't see the point in just pushing SQL into JPQL and expecting it to work.
What you can do with JPA 2.1 is invoke it as follows
function("date_format", s.date, '%Y, %m, %d')
where function is a way to invoke any native SQL function. This clearly means you lose database independence because that function is not valid on all datastores.
Sql function is available in JPQL. My JPA version is 1.11.9. Sample group by day query:
#Query(value = "SELECT count(ac) as count, function('date_format', max(ac.subscriptionStartDate), '%Y, %m, %d') as date FROM MyTable ac " +
"WHERE ac.subscriptionStartDate BETWEEN :startDate AND :endDate GROUP BY function('date_format', ac.subscriptionStartDate, '%Y, %m, %d')")
public List<Map<String,Object>> findRegisteredCustomersHistory(#Param("startDate") Date startDate, #Param("endDate") Date endDate);
The result of the query is the list that records the number of records grouped by days in formatted form.
Sample rows:
count: 3, date: 2019, 09, 10
count: 1, date: 2019, 09, 11
for your question, try this in JPQL:
#Query(value = "SELECT function('date_format', s.date, '%Y, %m, %d') as date from Transactions s;")

hql query to get last 1 month records

I need to fetch records of last month using hql earlier i used to fetch records using sql query but now i need to fetch records of 1 month span.for example today's date is 15-Dec-2014 I want to get records between 15-Nov-2014 to 14-Dec-2014 records.
Here is Mysql query:
SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM
F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN
SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW()
Frankly, I don't have any idea about how to write above query in HQl.
Can anyone please help me.
Could you try this, I couldn't test, if you it has a error please inform me;
String hqlQuery =" SELECT fds.EXISTED_PRODUCT_ID,fds.Product_Name,fds.PRODUCT_CREATED_DATE FROM F_PRODUCT_DATA_STATISTICS fds where fds.CREATED_TS BETWEEN DATE_SUB(current_date(), INTERVAL 7 DAY) AND current_date()";
query = session.createSQLQuery(hqlQuery);

show all records from mysql database table when date is not selected in java swing form

I am using java swing and mysql database for my desktop based application.
And i am having following query while giving date filter on reports.
If I have not enter To_date to From_date in JTextField then it must show all records from my mysql database table and if i have entered date then it must show records in between that two dates.
So i have tried (date between To_date and From_date) But its showing records only when i give dates to query otherwise empty.
Would you please suggest me something?
You should try some static value for date column if its null.
I hope below query will help you to resolve your issue.
SELECT * FROM YourTable WHERE IFNULL(#StartDateParameter, IFNULL(DateColumn,'1900-01-01')) <= IFNULL(DateColumn,'1900-01-01')
AND IFNULL(#EndDateParameter, IFNULL(DateColumn,'1900-01-01')) >= IFNULL(DateColumn,'1900-01-01')
Please let me know if you cant solve your issue with this query.
You can use IFNULL ( http://www.mysqltutorial.org/mysql-ifnull/ ) and form your query such that if your parameter is null then you should use your column value for comparison.
e.g.
SELECT * FROM YourTable WHERE IFNULL(DateColumn,#YourParameter) = #YourParameter

Select record based on date with MS Access

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

Categories

Resources