How to query for valid date which is in VARCHAR field using JPQL or Spring data jpa? - spring-boot

I have to get the min date from varchar field type.
I am able to do it using native query like below:
select * from license where TO_DAYS(STR_TO_DATE(license_date, '%Y-%m-%d')) IS NOT NULL ORDER BY STR_TO_DATE(verified_license_issued, '%Y-%m-%d') ASC LIMIT 1;
But I need to do it using JPA.
How to do it ?

Related

How to use count and row_number in java spring boot

I want to fetch the data in JPA Spring Boot from database using cast and count function, but my data is not returning any ID. So, I creating new field that returning auto number using row_number() in SQL.
I'm getting a problem in JPA Spring Boot to fetch the data using row_number,
here is my SQL Syntax
select row_number () over() as id,
cast (t.create_datetime as date) as reportDate,
count(*) as totalTransaction
from vx_transaction."transaction" t
group by cast(t.create_datetime as date)
the result in databases is as expected
Result in DBEAVER
but, I'm getting error in JPA Spring
ERROR: column "t.id" must appear in the GROUP BY clause or be used in
an aggregate function
Then, I'm trying to fix the problem with adding the id in group by. Here's the code
select row_number () over() as id,
cast (t.create_datetime as date) as reportDate,
count(*) as totalTransaction
from vx_transaction."transaction" t
group by cast(t.create_datetime as date), id
There is not error, but..... the result not as expected
Not as expected result
Can anyone help me to solve this problem??

how to get Null records from oracle database if column is Number type and nullable

I have Oracle database table with three columns i.e Id,RTOName,VehicleCode. my table looks like below
RTOName is the varchar2 type and VehicleCode is NUMBER(2,0) and is nullable field.
So I have the data like below and I want to fetch the records with Some VehicleCode and with null value. The table design like this already done so changing that will impact a lot in my application. I have a JPA Native Query that I used like this and I want to fetch the records with null values.
Query query = createNativeQuery("select RTOName,VehicleCode from tbl_vehiclecodes WHERE VehicleCode=#vCode");
query.setParameter("vCode", vehicleCode);
From above Query I will get only Non null valued record. Eg. for vCode parameter 61 I will get
Marathalli,61. If my vCode is null I have a problem and I wont get any record.
How to achieve this in Native Query?
I know that we can use IS NULL in the Query in where clause. Since I have some numbers here In my case how to solve this? Any help
Thanks
We can use OR here,
Following query will give you records with matched records for parameter vCode along with rows having null and in case of vCode is null you get the records only with null values.
Query query = createNativeQuery("select RTOName,VehicleCode from tbl_vehiclecodes WHERE (VehicleCode is null or VehicleCode=#vCode)");
Edit: considering the doubts from #Ranagal
If you want like in case of null value passed to vCode you want all the records having value in vehiclecode and also with null then we need to change the query like,
Query query = createNativeQuery("select RTOName,VehicleCode from tbl_vehiclecodes WHERE (VehicleCode is null or VehicleCode=coalesce(#vCode,VehicleCode))");

How can I convert a date to another format at run time in Oracle?

I have a date string coming from user input in the format of DD/MM/YYYY and I need to match it against a date column in our database in the format of DD-MON-YY.
Example input is 01/01/2015 and example date column in our database:
SELECT MAX(creation_date) FROM orders;
MAX(creation_date)
------------------
06-AUG-15
I need to query in the format:
SELECT * FROM orders WHERE creation_date = 01/01/2015
and somehow have that converted to 01-JAN-15.
Is it possible with some built-in Oracle function?
Use to_date, if the column in the table is in date format
http://www.techonthenet.com/oracle/functions/to_date.php
to_char allows you to specify different formats in a SQL statement.
Example: to_char(sysdate,'DD-MON-YYYY') will display 06-AUG-2015 for today's date.
TO_CHAR
Use to_date to compare your date column to a date string, but be careful in doing so since your date column may include a time component that isn't showing when selecting from your table.
If there is no index on your date column, you can truncate it during the comparison:
SELECT * FROM orders WHERE TRUNC(creation_date) = TO_DATE('01/01/2015','mm/dd/yyyy');
If there is an index on your date column and you still want to use it then use a ranged comparison:
SELECT * FROM orders
WHERE creation_date >= TO_DATE('01/01/2015','mm/dd/yyyy')
and creation_date < TO_DATE('01/01/2015','mm/dd/yyyy')+1;

like query on long datatype in hibernate

When I execute like query on oracle on number datatype , it works fine i.e
select * from customer c where c.customer_number like '%400%'
Here customer_number is of 'number' datatype in oracle.I have to execute same query from hibernate but in my POJO this customer_number maps to a Long type. I am able to do this by HQL
i.e
from Customer c where c.customerNumber like %400%
How can I do it with criteria query?
Thanks in advance
criteria.add(Restrictions.like('c.customerNumber', '400', MatchMode.ANYWHERE));
see http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/criterion/Restrictions.html#like%28java.lang.String,%20java.lang.String,%20org.hibernate.criterion.MatchMode%29

how to do in hibernate like order by to_date(ADD_DATE_CREATED,'DD-MM-YY') desc

i want to add criteria in hibernate for order by date.
Here date description in db as
vrhCreatedDate varchar2(20)
Here i have date with varchar datatype in database .
i m doing order by using
order by to_date(ADD_DATE_CREATED,'DD-MM-YY') desc
how to add criteria like below for order by date itself ?
criteria.addOrder(Order.desc("to_date({alias}.vrhCreatedDate, 'DD-MM-YY')")); //need to parse varchar column in date when pass order by column in hibernate.
Parse order by column with string to date field.
You can use the following statment to order by your date field
criteria.addOrder(Order.desc("your Field name"));

Resources