TIMESTAMP (6) WITH TIME ZONE to Time stamp - java

I have a query in my Java class that fetches (Order_num) VARCHAR and Time_Field(TIMESTAMP (6) WITH TIME ZONE) as shown below:
select order_num,time_Field from
MY_TABLE where ORD_NUM='123456789' ORDER BY time_Field desc
However it gives me "
No Dialect mapping for JDBC type: -101" exception
that I highly doubt is due to the mapping between Oracle database and Hibernate. Because without the "Time_Field" being returned the query runs fine.
Is there a way I can cast or convert the "TIMESTAMP WITH TIME ZONE" to simple "TIMESTAMP"?

I finally figured it out by running the below query:
SELECT order_num,TO_CHAR(
FROM_TZ( CAST( time_Field AS TIMESTAMP ), 'UTC' )
AT LOCAL,
'YYYY-MM-DD HH:MI:SS PM'
) AS local_time
FROM MY_TABLE
where ord_num='123456789'
ORDER BY time_Field desc;

Related

How to get current DB time from SQL Server using spring boot

I am using spring boot and SQL Server and getting unexpected end of subtree when I have the following code to get database time;
#Query("SELECT CURRENT_TIMESTAMP")
public Timestamp getTime();
SELECT CURRENT_TIMESTAMP works when ran in SQL Server though.
and when I do the following, I get the timestamp but in a list, based on how many rows the table has:
#Query("SELECT CURRENT_TIMESTAMP from MYTABLE")
public Timestamp getTime();
ok I fixed it by doing:
#Query(value = "SELECT CURRENT_TIMESTAMP", nativeQuery = true)
public Timestamp getTime();
The Java Persistence query language includes the built-in Datetime Functions, which may be used in the WHERE or HAVING clause of a query. Such as:-
functions_returning_datetime:=
CURRENT_DATE |
CURRENT_TIME |
CURRENT_TIMESTAMP
This way you can get it by:-
#Query(value = "SELECT CURRENT_TIMESTAMP", nativeQuery = true)
public Timestamp getTime();

Return CURRENT_TIMESTAMP value with specific timezone in Spring data JPA/Hibernate (HQL) query?

I have an Spring Boot API that uses Spring data JPA (1.5.9)/Hibernate (5.0.12) to query my PostgresQL database that is hosted on AWS as a RDS. It is set to Central Time (CST) I have some HQL (Hibernate) queries that use the CURRENT_TIMESTAMP function, but unfortunately and oddly seems to be returning UTC return values for whenever the HQL queries that use CURRENT_TIMESTAMP run.
I need a way to simply force the CURRENT_TIMESTAMP in the HQL query to be central time (CST). I was trying just querying the DB in pure SQL and something like this worked:
CURRENT_TIMESTAMP at TIME ZONE 'America/Chicago'
Unfortunately, I can't seem to get that to work in HQL, as IntelliJ/Hibernate throws a compilation error for:
<expression> GROUP, HAVING, or ORDER expected, got 'AT'
My sample HQL query I am using is:
#Query(value = "SELECT customerCoupons FROM CustomerCouponsEntity customerCoupons "
+ "WHERE customerCoupons.couponCode = :couponCode "
+ "AND customerCoupons.expiredDate >= CURRENT_TIMESTAMP "
+ "AND customerCoupons.startDate <= CURRENT TIMESTAMP "
)
List<CustomerCouponsEntity> findByCouponCode(#Param("couponCode") String couponCode);
Any help would be greatly appreciated. I have the DB set as CST in AWS, so I didn't even expect this CURRENT_TIMESTAMP to be returning a UTC value (still doesn't make sense to me, unless its somehow using the JDBC driver TimeZone or JVM? I mean, this is a Hibernate query, so its not pure SQL right?)
You should try to set the hibernate timezone in your spring boot properties file. Example:
spring.jpa.properties.hibernate.jdbc.time_zone=YOUR_TIMEZONE
Ensure that the value of YOUR_TIMEZONE matches your DB timezone.
I guess this article will help
Posting my own answer;
I tried setting the timezone in the properties/yaml per this article:
https://moelholm.com/blog/2016/11/09/spring-boot-controlling-timezones-with-hibernate
but it did not work no matter what I tried. I made sure I was on hibernate 5.2.3 or greater and it wouldn't work.
I also tried adding the "AT TIMEZONE" in my HQL query but the code wouldn't compile. I guess even though this is valid SQL it doesn't work with the Hibernate SQL queries i.e.
CURRENT_TIMESTAMP at TIME ZONE 'America/Chicago'
Anyway, the only thing that seemed to work was:
#PostConstruct
void started() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

JPA query does not return any data

I have a following JPA query:
#Query(value = "select r FROM TableEntity r where r.time=?1")
TableEntity findByTime(java.sql.Timestamp time);
That query is pretty straightforward, should fetch the database row based on some time. It works like a charm with MYSQL and Oracle but won't work for MSSQL. I have debugged the query through Hibernate and JTDS driver and saw that Timestamp is successfully resolved in the Prepared statement, I can see the exact number of hours, minutes, seconds and milliseconds in query as I have in the database row. However, no data is returned back.
class TableEntity {
#Type(type = "timestamp")
private Timestamp time;
}
I am suspecting that some milliseconds rounding happens or they somehow gets messed up since query does return something once in a blue moon!
My Time Field in the database is datetime2(3)
I am using the net.sourceforge.jtds 1.3 driver.
Time is formatted like this: 2020-06-03 13:02:21.273, I am working with milliseconds
EDIT:
I tried writing plain prepared statement and here are results:
select r FROM TableEntity r where r.time=?1
preparedStatement.setTimestamp(1, timestamp); //does not work...
preparedStatement.setString(1, timestamp.toString()) //works like a charm
Any idea?
So, under the hood, Hibernate was mapping that timestamp to datetime type when it queried the DB (I concluded that using the MSSQL profiler) . Since I had datetime2 in the DB, comparing datetime with datetime2 on the DB level was not returning anything even if they were exactly the same in Milliseconds.
Solution would be to force mapping of timestamp to datetime2 so db datetime2 type gets queried with the same type

Unexpected token "(" - Java -- SQL Server

I am using the Niagara 4 Framework (Java) where I want to send queries to my SQL Server database.
Here is my query : (it does work in SQL Server)
SELECT * FROM [RESTART]
WHERE TIMESTAMP > CURRENT_TIMESTAMP and TIMESTAMP <DATEADD(minute, 10,
CURRENT_TIMESTAMP )
And I get the following error :
Syntax error near:"DATEADD(" Unexpected token "(" at line 0, column 11.
Any idea why do I get this ?
your code with the table name and field name switch to a table I have executes with out error in SSMS,
SELECT * FROM [OutgoingAudit]
WHERE CreateTS > CURRENT_TIMESTAMP and CreateTS < DATEADD(minute, 20, CURRENT_TIMESTAMP )
that being said the first part of your where
WHERE TIMESTAMP > CURRENT_TIMESTAMP
unless TIMESTAMP is a future it will never happen, but that may be what you are looking for
TIMESTAMP and CURRENT_TIMESTAMP are both reserved words in SQL Server.
Try changing TIMSTAMP to MY_TIMESTAMP
Also, contrary to what the name suggests, TIMESTAMP is not actually a time, but CURRENT_TIMESTAMP is...
The TIMESTAMP data type is just an incrementing number and does not preserve a date or a time.
and
CURRENT_TIMESTAMP returns the current database system timestamp as a datetime value without the database time zone offset.
So comparing a TIMESTAMP (not a time) to CURRENT_TIMESTAMP (yes a time) is likely causing problems.

MySQL check case where a date is 0000-00-00 00:00:00?

I have this MySQL query:
SELECT `date_joined`, `date_last_joined` FROM `users` WHERE `name` = "mary"
The date_last_joined column is currently 0000-00-00 00:00:00 for all users. It's then updated to the current date next time they log in.
So my question is, how would I return the date_joined if date_last_joined is 0000-00-00 00:00:00, and date_last_joined if it isn't? Is this possible in a MySQL query?
The reasons for this are that returning 0000-00-00 00:00:00 in a Java prepared MySQL query causes all sorts of issues.
You can use the IF flow control operator:
SELECT IF(`users`.`date_last_joined` = '0000-00-00 00:00:00',
`users`.`date_joined`, `users`.`date_last_joined`) AS `join_date`
FROM `users`
WHERE `users`.`name` = 'mary'

Categories