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.
Related
I have a column of type varchar which stores date with timestamp, and without timestamp in postgres.
While trying to hit the below query,
select * from table where cast(column as date) = CURRENT_DATE::date
it is returning those columns also which has date with timestamp also. How can I modify the query to find the exact match of only dates and not with timestamp.
sample input data:
Column1
2022-12-09 17:38:53.415367
2022-12-09
Expected output:
2022-12-09
Actual: getting both the columns in the result
2022-12-09 17:38:53.415367
2022-12-09
Per my comment:
SELECT
t.dt_val
FROM (
VALUES ('2022-12-09'),
('2022-12-09 17:38:53.415367')) AS t (dt_val)
WHERE
t.dt_val::timestamp = '2022-12-09'::date + '00:00:00'::time;
dt_val
------------
2022-12-09
I used '2022-12-09'::date instead of current_date to make the answer relevant in the future. At time of answer current_date resolves to '2022-12-09'::date.
I have this db table in an H2 database called "mytable" and two of the fields have the type "TIMESTAMP".
I have writen the following query in order to update the table:
UPDATE mytable
SET START_TIME_ = "2018-01-01 01:01:01" , END_TIME_ = "2020-01-01 01:01:01";
I was wondering how could I modify my query so that it generates at the START_TIME_ and END_TIME_ fields random dates between 2018-01-01 01:01:01 and 2020-01-01 01:01:01. As the H2 timestamp is not a UNIX timestamp I am unsure how to go about it. I appreciate any help you can provide.
In H2 1.4.200 you can use
UPDATE mytable SET
START_TIME_ = #T := TIMESTAMP '2018-01-01 01:01:01'
+ RAND() * INTERVAL '730 00:00:00' DAY TO SECOND,
END_TIME_ = #T + (TIMESTAMP '2020-01-01 01:01:01' - #T) * RAND();
INTERVAL '730 00:00:00' DAY TO SECOND can be replaced with the subtraction operation between high and low bounds (TIMESTAMP '2020-01-01 01:01:01' - TIMESTAMP '2018-01-01 01:01:01').
Note that distribution of START_TIME_ is linear here, but distribution of END_TIME_ is not. If such distribution doesn't satisfy your needs, you need to use some more complex expressions, but you can use the same datetime arithmetic operations in them.
Please also note that inline variable assignment syntax uses the := operator and not the = operator.
I created table column datatype as timestamp while storing it stores the default timestamp format yyyy-mm-dd HH:mm:ssZbut when try to select based on timestamp it doesn't return record.
CREATE TABLE TEST
(
TS timestamp,
VALUE text,
EMAILID text,
PRIMARY KEY (TS,VALUE)
);
INSERT INTO TEST(TS,VALUE,EMAILID) VALUES('1418026180922','sometext','email#');
SELECT * FROM TEST WHERE TS='2014-12-08 00:38:10-0800' ALLOW FILTERING;
THIS Query returns 0 rows? Why it returns like that am i doing something wrong?
It works for below query:
SELECT * FROM TEST WHERE TS='1418026180922' ALLOW FILTERING;
Your query
SELECT * FROM TEST WHERE TS='2014-12-08 00:38:10-0800' ALLOW FILTERING;
discards (or rather ignores) the millisecond part of the timestamp you use to insert the row
1418026180922
// ^^^^
And so the dates aren't equal.
If you had instead inserted
INSERT INTO TEST(TS,VALUE,EMAILID) VALUES('14180261800000','sometext','email#');
you could retrieve it with
select * from test where ts='2014-12-08 00:09:40-0800'; // note that I've fixed it from your '2014-12-08 00:38:10-0800'
I have a table , there are 4 fields: name, start date and time (Timestamp) ,end date and time (timpestamp) , car.
In Java/MySQL,
I need to compare database start date time and end start date time and compare with value given in textbox date time field .
Now problem is that we need to book person(driver) and car, if car and person
are not booked in given time (that is checked by database) , then we can booked it else not.
Please tell me a logic/query to do this.
If you have code please mention it.
Try this query,
SELECT name FROM table having '2014-11-28' between start_date and end_date;
You can use this query to check availability.
SELECT name FROM table WHERE start_date < '2014-03-13 11:42:28' AND end_date > '2014-03-13 11:42:28' limit 1
If you get any name, then you can not book new driver. Also look at mysql date and time functions.
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));