JPA/Hibernate, Query timestamp column ignores time part of criteria - sql

I'm using JPA/Hibernate combination to store entities to an oracle database.
The table contains a TIMESTAMP(6) column to store a date value with time and milliseconds.
Storing and loading the entity works as intended. The "time" part of the java.util.Date field in the entity is processed correctly.
But when I query the table with a native query and where time < :startTime condition the startTime only contains date '30-Jul-14'. The "time" part is truncated.
Hibernates query look like:
select documentta0_.id as id1_14_, documentta0_.id_document as id5_14_, documentta0_.host as host2_14_, documentta0_.task as task3_14_, documentta0_.time as time4_14_
from document_task documentta0_
where documentta0_.task='preview'
and documentta0_.host='my.host.uk'
and documentta0_.time>'30-Jul-14'
order by documentta0_.time ASC
The orm.xml File contains the following column definition:
<basic name="time" optional="false">
<column name="time"/>
<temporal>TIMESTAMP</temporal>
</basic>
How to query with date and time?

Maybe it is just a matter of formatting. In case that you are using something like p6spy, try uncommenting and setting the property to
databaseDialectDateFormat=dd-MM-yy HH:mm:ss.SSSSSS

Related

SQL for a specific date in Oracle table

I have a Oracle table with a DATE field, called UTC_DT. If I do the following query, I get proper results:
select *
from t_table
where trunc(UTC_DT) = to_date('20200406000000','YYYYMMDDHH24MISS');
But no result if I set some hour/min like in the following:
select *
from t_table
where trunc(UTC_DT) = to_date('20200406182000','YYYYMMDDHH24MISS');
Shouldn't this work as we convert both side to a date without time?
You seem confused between data types; perhaps you have worked in a different database environment in the past, with different data types for "dates" and "times".
Oracle's date data type has a misleading name - it should be called "date-time" because it always includes a time component. to_date() converts from string to date, and the result always has a time component. Even when you convert a string like '20200406' with format model 'yyyymmdd', with no time-of-day components; the default of '000000' for 'hh24miss' is added automatically. to_date does not truncate from "date-time" to "date" as you seem to expect (presumably without time component - which may exist in other database products, but not in Oracle).

Sql Query using 'Like' is giving results but using '=' does not returns any result in Oracle

The Query using LIKE :(This query when fired gives the desired result)
select * from catissue_audit_event where event_timestamp like '16-DEC-14'
But when using query with '=' results in an empty resultset
select * from catissue_audit_event where event_timestamp='16-DEC-14'
Here event_timestamp is of type Date
Strange thing is that the query runs for other dates such as:
select * from catissue_audit_event where event_timestamp='15-DEC-14'
What can be the issue? I already checked for leading and trailing spaces in the data
Output after running the first query:
In Oracle a DATE (and of course a TIMESTAMP) column contains a time part as well.
Just because your SQL client is hiding the time, doesn't mean it isn't there.
If you want all rows from a specific day (ignoring the time) you need to use trunc()
select *
from catissue_audit_event
where trunc(event_timestamp) = DATE '2014-12-16';
Be aware that this query will not use an index on the event_timestamp column.
You should also not rely on implicit data type conversion as you do with the expression event_timestamp = '16-DEC-14. That statement is going to fail if I run it from my computer because of different NLS settings. Always use a proper DATE literal (as I have done in my statement). If you don't like the unambiguous ISO date, then use to_date():
where trunc(event_timestamp) = to_date('16-12-2014', 'dd-mm-yyyy');
You should avoid using month names unless you know that all environments (which includes computers and SQL clients) where your SQL statement is executed are using the same NLS settings. If you are sure, you can use e.g. to_date('16-DEC-14', 'dd-mon-yy')
The reason why this is different is different to the solution to your issue.
The solution to your issue is to stop performing date comparisons by implicit conversion to a string. Convert your string to a date to perform a date comparison:
select * from catissue_audit_event where event_timestamp = date '2014-12-16'
I cannot stress this enough; when performing a date comparison only compare dates.
Your column EVENT_TIMESTAMP is being implicitly (this is bad) converted to a date in accordance with your NLS_DATE_FORMAT, which you can find as follows:
select * from nls_session_parameters
This governs how date-data is displayed and implicitly converted. The reason why LIKE works and and = doesn't is because your NLS_DATE_FORMAT is masking additional data. In other words, your date has a time component.
If you run the following and then re-select the data from your table you'll see the additional time component
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss'
Thus, if you want all the data for a specific date without constraint on time you'll need to remove the time component:
select * from catissue_audit_event where trunc(event_timestamp) = date '2014-12-16'
have you tried matching the event_timestamp format example: DD-MMM-YY with the date that you are passing?

SQL Query "time" Reference

I've got the below query in MS SQL Server Management Studio:
SELECT t1.time, value, annotations
FROM PI.piarchive..picomp2 t1
WHERE tag = 'sinusoid'
AND t1.time >= 't'
AND annotated = 1
Unfortunately, when I try to run the query, the below error is returned:
"Conversion failed when converting date and/or time from character string."
That tells me that it's trying to use the SQL in-built time reference, but preventing me from referring to the "time" attribute in the system table "PI.piarchive..picomp2".
Are you able to advise what changes in syntax I need to make to change the behaviour during query execution so it can query the "time" attribute in the "PI.piarchive..picomp2" table?
EDITED
The "time" attribute is of DateTime type, but since this is a historian I am querying via OLEDB, the reference of 't' (what I am trying to compare with) is a valid value as 't' refers to today.
As it's said in the error message you are trying to compare value of data type datetime and character string. Of course, that's not aloud. How can you compare for example word 'ostrich' and current date? Which one is bigger or less?
You can compare t1.time with current date this way (SQL Server 2008+):
t1.time >= CAST(GETDATE() as date)

Update table Error Using Convert Function In SQL Server 2005

I have a table with two columns, all of them are datetime value
Such as, Column A with value ‘07/09/2012 14:13:34’
Now, I want to update column A to yyyymmdd by statement
Update Change_Date
SET A = CONVERT(VARCHAR(8),A,112)
It shows succsessful message but with no effect (no update value to 20120907) in my table Change_Date.
Any help will be greated, thank you!
A datetime fields saves a date time. How you see that date time is a result of the tool you're using to inspect the data, whether it is Management Studio, or your own software that's printing something from the database.
I strongly recommend keeping it as a datetime field. This will allow you to do date-related operations, such as subtractions and comparisons. If you want to change how your users see the date, then format your date at the presentation layer.
What's happening in the code you've posted is that you're setting the value of A to the same date that it already is. The fact that you're setting that value by means of a string in another format has no relation, SQL server will always have to parse your string input into a date that it can understand. This is why you're not getting an error message. The operation is working, only it's not changing anything.
You can select the date column in specified format or make a view which selects the column value in yyyymmdd format:
SELECT CONVERT(VARCHAR(8), A, 112) FROM Change_Date
It's because the datatype of the column is DATE or DATETIME and it has specific format. If you want to update the column with specific format, make another column and make its datatype VARCHAR. I believe 112 is yyyymmdd format.
I strongly suggest that you keep it AS IS. Database is the storage of data and not for viewing purposes. It is easy to perform task for dates if your data type is DATETIME or DATE. If for instance you want to retrieve the dates with specific format, that's the time you convert your date.
Hope this makes sense.

Does constructor DateTime take string or ONLY TIMESTAMP type as parameter?

I am working with SQLite SQL statement or Query.
So far, I was able to figure out some of things by asking stackoverflow question that can cause problem when executing such SQL Statement.
However, I still am not able to get any result?
Here is my SQL Query:
SELECT *
FROM Alarms
WHERE ALARMSTATE IN (0,1,2)
AND ALARMPRIORITY IN (0,1,2,3,4)
AND ALARMGROUP IN (0,1,2,3,4,5,6,7)
AND DateTime(ALARMTIME) BETWEEN datetime("2012-08-02 00:00:00")
AND datetime("2012-08-03 00:00:00")
ORDER BY ALARMTIME DESC
Here is my table as viewed within datagridView control:
As you can see, I have records from yesterdays and the day before in the table.
I corrected all the columns in question of their format such as my ALARMTIME, which is TEXT in data type. Still not getting any result, I decided to run the SQL statement right from within SQLite Administrator application as below. Surprisingly, I got the same result which is nothing or nill or null for dataset. However, SQLite Administrator showed me that DateTime accepts only TIMESTAMP as a parameter not TEXT. If so, my above SQL Statement won't work even if I have everything else correct. Am I correct in saying that?
Here is my answer. From what I understand, DATETIME supposed to take string or timestamp as a parameter, but in my case it doesn't work for me.
Since my column ALARMTIME is TEXT, I am able to query my table with the following SQL statement and retrieve dataset I am looking for.
SELECT *
FROM Alarms
WHERE ALARMSTATE IN (0,1,2)
AND ALARMPRIORITY IN (0,1,2,3,4)
AND ALARMGROUP IN (0,1,2,3,4,5,6,7)
AND ALARMTIME LIKE "2012/08/01%"
ORDER BY ALARMTIME DESC
I simply can't convert my ALARMTIME text into datetime type.
try using between 8/1 and 8/3 and you will get the ones for 8/2.
the way you have it should pull up the alarms in between the two times though
EDIT
can you change your ALARMTIME to a DATETIME format? Then you would be able to use the Between and you wouldn't have to convert the text to DATETIME.
if you Convert to DATETIME type you will have better flexibility in your query. it will be easier to use in the future as well.
I believe that DATETIME is a overloaded text type, so it should output text