search record using year criteria in hibernate - oracle

I need all records which have year entered from search criteria.
for ex: String year = "2012";
In hibernate how it work ?
criteria.add(Restrictions.like("createDate", "%"+year+"%"));
createDate is fetch in format DD-MM-YY from oracle database.
Please suggest..

Try something like:
criteria.add(Restrictions.sqlRestriction("to_char({alias}.CREATE_DATE, 'yyyy') = ?", year, Hibernate.STRING));
I'm assuming that the name of the column mapped to createDate is CREATE_DATE

Related

Spring MongoRepository filter string date column

Is it possible to filter a MongoRepository in Spring where there is a column with date strings like that:
2019-01-01T00:00:00
2022-01-20T00:00:00
2022-05-01T00:00:00
I try to get all columns where the Date value is after a certain date.
I tried the following statements:
findAllByPersonIdAndDateAfter(int id, Date after);
findAllByDateAfter(Date date)
findAllByDateGreaterThan(Date date);
Invoked it with e.g.:
Date after = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2022-03-31 23:00:00");
repository.findAllByDateAfter(after)
But it always returns 0 rows.

java.sql.SQLDataException: ORA-01830: date format picture ends before converting entire input string (Oracle)

I'm trying to run a script through Java code
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE TO_DATE(CREATEDATE, 'DD-MM-RR') = TO_DATE('26-NOV-19', 'DD-MM-RR')\";");
stmt = con.prepareStatement(query.toString());
rs = stmt.executeQuery();
When trying to run the script in Oracle SQL Developer it works fine, but when running my java code it gives an SQLDataException:
java.sql.SQLDataException: ORA-01830: date format picture ends before converting entire input string
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:440)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:837)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:445)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:191)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:523)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:863)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1153)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1275)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3576)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3620)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
I can't find whats causing the error, please anyone? Thank you
Assuming that CREATEDATE is a column of the DATE data type then you do not need to use TO_DATE on it (as it is already a DATE), you also do not need the trailing \";.
You could also use a date literal.
So assuming that CREATEDATE always has a mignight time component:
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE CREATEDATE = DATE '2019-11-26'");
If your CREATEDATE column has a non-midnight time component then you need to compare on a range (or use TRUNC but that would not allow you to use an index on the column and would require a separate function-based index):
query.append("SELECT CNAME,CREATEDATE FROM MERGE_DATA_VW \r\n" +
"WHERE CREATEDATE >= DATE '2019-11-26' AND CREATEDATE < DATE '2019-11-27'");
I don' think you need TO_DATE for CREATEDATE as it is already a date.
Also, your TO_DATE('26-NOV-19', 'DD-MM-RR') should be TO_DATE('26-NOV-19', 'DD-MON-RR')
Try this:
SELECT
CNAME,
CREATEDATE
FROM
MERGE_DATA_VW
WHERE
TRUNC(CREATEDATE) = TO_DATE('26-NOV-19', 'DD-MON-RR');
Cheers!!
Wrong date format
TO_DATE('26-NOV-19', 'DD-MM-RR')
Please user Correct date
ie 'DD-MON-RR' FOR '26-NOV-19'
You where caluse should look like -
WHERE TO_DATE(CREATEDATE, 'DD-MM-RR') = TO_DATE('26-11-19', 'DD-MM-RR')

Date format in Oracle- fetching Date of certain range

I have a date table in my db in Oracle. When I run a query I get the date format as '01-05-2015' but when I run a similar query in BIRT, I get the date format as '01-MAY-2015 12:00 AM'. How can I get the date format in dd/mm/yyy by keeping the data type of date field as date.
here is sample of my database.
EQ_DT
05-07-2015
06-06-2015
15-02-2015
19-09-2015
28-12-2015
also my query is :
select to_date(to_char(to_date(enquiry_dt,'DD/MM/YYYY'),'DD/MM/YY'),'DD/MM/YY') as q from xxcus.XXACL_SALES_ENQ_DATAMART where to_date(to_char(to_date(enquiry_dt,'DD/MM/YY'),'DD/MM/YY'),'DD/MM/YY')>'21-06-2012' order by q
I am getting error of NOT A VALID Month also
If enquiry_dt is already a date column, why are you trying to convert it to date (and then to char and to date again)?
SELECT to_char(enquiry_dt, 'DD/MM/YYYY') AS q
FROM xxcus.xxacl_sales_enq_datamart
WHERE enquiry_dt > to_date('21-06-2012', 'dd-mm-yyyy')
ORDER BY enquiry_dt
In birt, where you place the field on the report, set the field type to date. Then in properties for that field , go to format date time, and finally specify the date formatting you want for that field .
I prefer to always use pass date parameters as strings to BIRT, using a known date format. This is for report parameters as well as for DataSet parameters.
Then, inside the query, I convert to date like this:
with params as
( select to_date(pi_start_date_str, 'DD.MM.YYYY') as start_date_incl,
to_date(pi_end_date_str, 'DD.MM.YYYY') + 1 as end_date_excl
from dual
)
select whatever
from my_table, params
where ( my_table.event_date >= params.start_date_incl
and
my_table.end_date < params.start_date_excl
)
This works independent of the time of day.
This way, e.g. to select all events for january 2016, I could pass the query parameters '01.01.2016' and '31.01.2016' (I'm using german date format here).

Hibernate criteria for get records which have year 2012

i need all records which have year entered from search criteria.
for ex:
String year = "2012";
In hibernate it work for
criteria.add(Restrictions.like("createDate", "%"+year+"%"));
createDate is in format DD-MM-YY.
Please suggest..
criteria.add(Restrictions.like("createDate", "%2012%", MatchMode.ANYWHERE));
But I would prefer changing your db field to a date format(its string at the moment?) and performing a greater than comparison (instead of like).
like only applies for the String . For the date field , you can use Restrictions.between() to generate between '01/01/2012 00:00:00' and '12/31/2012 23:59:59' for the where clause
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date fromDate = df.parse("1-1-2012 00:00:00");
Date toDate = df.parse("31-12-2012 23:59:59");
criteria.add(Restrictions.between("createDate", fromDate, toDate));
Also , please note that all the properties used in the Criteria API is the Java property name , but not the actual column name.

Group by using month, year in doctrine2

How to write a following SQL query in doctrine2 as DQL .
SELECT COUNT(id)
FROM stats
WHERE YEAR(record_date) = 2009
GROUP BY YEAR(record_date), MONTH(record_date)
i.e i would like to group by results based on month,year of datetime field stored in MySQL table.
In DQL you could also group by month, year, day etc with SUBSTRING.
For example - group by month (datetime format Y-m-d H:i:s):
SELECT p, SUBSTRING(p.date, 6, 2) as month
FROM Entity p
GROUP BY month

Resources