current_date is give search result with a timestamp column in postgres - java

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.

Related

how to generate random timestamp in sql query withing range for h2 database?

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.

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.

get missing dates from a date column in a table darby apache db

I have a table name as 'h1' with field as 'date'. How do I get missing dates in this table using Darby query
DATA IN TABLE h1
date
------------
2018-03-10
2018-03-11
2018-03-13
2018-03-14
2018-03-16
Expected Result is missing dates: 2018-03-12 ,2018-03-15
I've tried every thing on blogs but nothing found properly working.
If you're just looking for a query, something like this might work.
SELECT sq.nextday AS missingday
FROM
(SELECT date1.dates AS currentdate,
date1.dates + INTERVAL 1 DAY AS nextday,
date2.dates AS actualnextday
FROM h1 date1
LEFT JOIN h1 date2 ON date2.dates = date1.dates + INTERVAL 1 DAY) sq
WHERE sq.actualnextday IS NULL HAVING max(sq.currentdate) > sq.nextday;

Hibernate Query search criteria filter by timestamp

Good day,
I have a db2 database, and I have a sql query as follow:
select * FROM SCORPORATEREGISTEREDACCOUNTS CRA WHERE cra.corporateRegisteredAccountId = 241
and cra.paymentDate >= '2017-07-07 00:00:00' and cra.paymentDate <= '2017-07-07 23:59:59';
And in my database, the row record having paymentDate = '2017-07-07 11:48:00'. This paymentDate column is set as TIMESTAMP for its data type.
When I run this query through command, or through dbVisualizer, I can get the result correctly.
However, When run this query through java code using Hibernate Query, I will have some weird result, which is getting null record for the first time, and have record on the second time and onward, and If I log out from my java web application, and log in again, same problem occur.
Here is my sample code that trigger by a search button:
StringBuilder sb = new StringBuilder( );
sb.append( " \\ my query here " );
Query query = getSession( ).createSQLQuery( sb.toString( ) );
return query.list();
In other word, I have to press the search button second time only can get the correct result.
And here is some of my test case:
If I change the row record paymentDate to '2017-07-07 00:00:00', then it wont have issue for searching.
If I change my sql query to search within 2 day instead of 1 day, then it wont have issue also.
where cra.paymentDate >= '2017-07-07 00:00:00' and cra.paymentDate <= '2017-07-07 23:59:59'
Or I change my code to call the query.list() 2 times (only return at second time):
query.list();
return query.list();
However, my solution is cast the column to date:
where date(cra.paymentDate) >= '2017-07-07 00:00:00' and date(cra.paymentDate) <= '2017-07-07 23:59:59'
My question is, how come the original query wont work for first time, and only work for second time. If think as logic, it should fulfill the search criteria and return me correct result. Is it a Hibernate Query bugs? Or there is other reason or something wrong with my code.
Kindly advise.

Cassandra time stamp returns 0 rows

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'

Categories

Resources