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'
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 a table with some data, there are record id, userIds, timestamp and data columns. Receive from the client a list of userIds, initially, I just had to fetch data by the same timestamp range for all useIds, just using userId IN (list). However, now I'm required to get data by different timestamp ranges for each userId, let's say userID 1 needs data from 1643580000000 to 1646431200000 and userId 2 from 1626418800000 to 1647500400000 (utcTimestamp in mills).
Most probably I'll receive a list of [userId, startTime, endTime], so, I was considering to loop the main query for each userId with its respective timestamp range (I've seen it's a bad idea due to performance, but if I have to, I have to), but I also found out about cursors (not much experience here).
I wanted to know if it's possible to get what I want without loops or cursors, and if not, best way with each one.
Thanks in advance!
Notes: using MariaDB. SQL query will be used as nativeQuery in a Java service repository.
You can run a query laike this.
Of course you have to add for every user his own time range
SELECT * FROM mytable
WHERE (userID = 1 AND `timestamp` BETWEEN 1643580000000 AND 1646431200000)
OR (userId = 2 AND `timestamp`BETWEEN 1626418800000 AND 1647500400000)
If you have a lot of rows and not that many id to process, you can do it with
SELECT * FROM mytable
WHERE (userID = 1 AND `timestamp` BETWEEN 1643580000000 AND 1646431200000)
UNION
SELECT * FROM mytable
WHERE (userId = 2 AND `timestamp`BETWEEN 1626418800000 AND 1647500400000)
Here also you need to add for every id another UNION
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.
I want to insert and retrieve a decimal value in hive table.
Here is my snippet which always returns NULL:
CREATE TABLE complex_types (c1 decimal);
INSERT INTO TABLE complex_types SELECT cast('100' as decimal);
SELECT * FROM complex_types;
You can read and write values in such a table using either the LazySimpleSerDe or the LazyBinarySerDe.
For example:
alter table decimal_1 set serde 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe';
or
alter table decimal_1 set serde 'org.apache.hadoop.hive.serde2.lazy.LazyBinarySerDe';
source information is here
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