How do I convert the following format to UNIX timestamps?
A value like: 01-02-2015 10:20 PM should be converted to: 1418273999000
I did try to_timestamp function but its not working for me.
If your data is stored in a column called ts, in a table called data, do this:
select extract(epoch from ts) from data
To add Joe's answer, you can use date_part, i think it's syntax is clearer than 'extract'.
select date_part('epoch', ts) from data;
Adding to haoming answer,
for UNIX epoch this was my approach.
I also added a 180 day interval which can be changed/removed upon requirements.
date_part('epoch', (column_name + INTERVAL '180 day')) * 1000
Related
It sounded like a simple problem, but I found no easy solution online.
I am trying to replicate the current ORDER BY in Hibernate, without succes:
SELECT * FROM IPEM.DEMANDE
WHERE INIT_DATE >= TO_TIMESTAMP('23/04/2021', 'dd/MM/yyyy') AND INIT_DATE <=
TO_TIMESTAMP('29/04/2021', 'dd/MM/yyyy')
ORDER BY TO_TIMESTAMP(LIMIT_DATE, 'dd/MM/yyyy'), TO_TIMESTAMP(INIT_DATE , 'dd/MM/yyyy') ASC <<< this line
Why ? Because I have this kind of data in my database:
05/05/2021 00:00:00 - 23/04/2021 00:00:00
05/05/2021 00:00:00 - 28/04/2021 00:00:00 << this should be 3rd
05/05/2021 02:00:00 - 24/04/2021 00:00:00 << this should be 2nd
The hours mess up the sorting. I'm trying to ignore them/format the date before loading my entries. A way I found to do so is applying TO_TIMESTAMP to ORDER_BY. It works well in SQL, but when going to Hibernate, it is not that simple.
Actually, my code looks like this:
criteria.addOrder(Order.asc(fieldName));
I tried some trivial solution, which obviously did not work:
criteria.addOrder(Order.asc("TO_TIMESTAMP(" + fieldName + ", 'dd/MM/yyyy')");
For which I had the following error (limitDate is the Java name, LIMIT_DATE the corresponding column):
org.hibernate.QueryException: could not resolve property: TO_TIMESTAMP(limitDate, 'dd/MM/yyyy')
How can I apply TO_TIMESTAMP to the members of my ORDER BY ? (No worries, we assume here all the members are dates, both in Java and in the SQL Table).
That's not possible, but you can subclass Order and override the org.hibernate.criterion.Order#toSqlString method to implement whatever logic you need. Anyway, you should move away from the legacy Criteria API to the JPA Criteria API. There you could use criteriaQuery.orderBy(criteriaBuilder.function("TO_TIMESTAMP", root.get(fieldName), criteriaBuilder.literal("dd/MM/yyyy")))
I need an way of to calculate the difference of a range of dates to other dates already persisted in database, i.e.:
As arguments I would like to inform something like:
from 15/JAN/2013 to 27/JAN/2013
In database/collection I have:
START_DATE, END_DATE
-------------------------------------------
01/JAN/2013 00:00:00, 17/JAN/2013 22:30:00
23/JAN/2013 17:00:00, 31/JAN/2013 23:59:00
And the return must be:
17/JAN/2013 22:30:01, 23/JAN/2013 16:59:59
REASON:
I have files that contains up to 40 thousand rows in range of dates. Files that intersect between dates contains the same data. This is why I need to insert only data in date range gaps.
Thanks to everyone that could help me!
select start_gap, end_gap
from(
select
end_date start_gap,
lead(start_date) over (order by start_date) as end_gap
from your_table
)
where end_gap is not null; --last line is not interesting
The question is unclear. Use Between operator: where dates between 15/JAN/2013 and 27/JAN/2013. And you will get exactly what is in your return.
Try this:
select START_DATE,END_DATE from tablename where START_DATE>max(START_DATE) and END_DATE<max(END_DATE)
I believe this has been a bug/problem in SQL 2000/2005 ... If my results have null on DATETIME column, i get
com.microsoft.sqlserver.jdbc.sqlserverexception:
the conversion from int to date is unsupported
when i use sql.getDate("ColumnName") ...
Any solutions to this?
[EDIT]
Hi all thanks for your inputs, below is my SQL query
select p.planno as PlanNumber,
t.TrancheID as TrancheID,
t.tranchestartdate as TrancheStartDate,
t.tranchereasoncode as TrancheReasonCode,
ai.ArrayItemDecode TrancheReasonDescription,
t.trancheuwstage as UnderwritingStatusCode
from plan_ p
inner join tranche t
on t.planno = p.planno
and t.trancheuwstage in ( 2, 4 )
and p.planno = '040000000X6'
inner join arrayitem ai
on ai.ArrayNm = 'arrTraReas'
and ai.ArrayItemCode = t.tranchereasoncode;
and the culprit here is tranchestartdate which is a DATETIME. I can't really add anything to tabel as i'm not allowed to change existing table structures, this is a big system. Perhaps i can do the casting in my SQL? I'm not quite sure if this is definitely a null problem.. Can one debug/watch through the ResultSet and check if any data was retrieved before i call getDate()?
[/EIDT]
If your application (or drivers) cannot handle null dates then the easiest thing might be to use ISNULL(field, <null replacement date>) and check for the null replacement date in code. This approach uses a magic number (date) to indicate null values, true. It's not pretty but it is quick and straightforward.
Can't you use coalesce for this?
http://msdn.microsoft.com/en-us/library/ms190349.aspx
Are you selecting the datetime from a field or constructing it dynamically in your SELECT? you may have to CAST the result.
See this article
I have the following SQL that returns the max BILL_DATE based on some criteria. BILL_DATE is defined in the database as a DATE.
SELECT MAX(BILL_DATE)
FROM BILLTABLE
WHERE col1 = ? and
col2 = ?
But when I read the value from the resultSet.
bill.setBillDate(resultSet.getDate(1));
An exception is thrown:
Invalid data conversion: Wrong result column type for requested conversion. ERRORCODE=-4461, SQLSTATE=42815
I have also tried
bill.setBillDate(resultSet.getString(1));
But that doesn't return a date. It returns either 100, 200 or 300 which is obviously not correct.
Is there another way to do this? Am I doing something wrong?
Ash is right, how are you defining the date column?
Is it possible the column is timestamp? In that case try resultSet.getTimestamp(1))
I had two resultSets open in the function where I was reading in the BILL_DATE. I changed the code to the following and it works fine.
bill.setBillDate(resultSet1.getDate(1));
Previously the column Data type is Date now I am changed to Timestamp
Now if I tried to run the program am getting them exception
java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 12 to TIMESTAMP.
at com.mysql.jdbc.ResultSetRow.getTimestampFast(ResultSetRow.java:1298)
at com.mysql.jdbc.ByteArrayRow.getTimestampFast(ByteArrayRow.java:124)
at com.mysql.jdbc.ResultSetImpl.getTimestampInternal(ResultSetImpl.java:6610)
at com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5928)
at com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5966)
at org.hibernate.type.TimestampType.get(TimestampType.java:30)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
... 40 more
You can just add zeroDateTimeBehavior=convertToNull to your connection jdbc:mysql://localhost/test?zeroDateTimeBehavior=convertToNull.
For me, it works perfectly.
pls refer to this link for more detail.
0000-00-00 00:00:00 is outside the range of a TIMESTAMP value (in fact, it won't work with a DATE field either). From the MySQL manual:
The TIMESTAMP data type has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
You can Use UNIX_TIMESTAMP(date) function to explicitly convert the value to TIMESTAMP.
I'm going to take a wild guess here that you're using MySQL :-) It uses "zero dates" as special placeholder - unfortunatelly, JDBC can not handle them by default.
The solution is to specify "zeroDateTimeBehavior=convertToNull" as parameter to your MySQL connection (either in datasource URL or as an additional property), e.g.:
jdbc:mysql://localhost/myDatabase?zeroDateTimeBehavior=convertToNull
This will cause all such values to be retrieved as NULLs.
Make sure that in you java code the field type is java.sql.Timestamp