JOOQ how query for between dates - mysql

I'm using jooq in my project and I need to query some data between two dates.
The sql query which produces right data is
select created_on from queue_token where created_on between '2015-07-16' and '2015-07-17' and token_queue_id=1;
the equivalent jooq query which i have written is below but doesn't give out the required result
create.select().from(com.database.tables.QueueToken.QUEUE_TOKEN)
.where(com.database.tables.QueueToken.QUEUE_TOKEN.TOKEN_QUEUE_ID.equal(1))
.and(com.database.tables.QueueToken.QUEUE_TOKEN.CREATED_ON.between(new Timestamp(fromDate.getTime())).and(new Timestamp(toDate.getTime())))
.fetch();
The jooq query produces result but only produces records that exactly match the fromDate. So basically it's not working for the date range.
Can somebody help here?

I think the problem is in passing a timestamp or date and time (I do not know java well). So instead sending e.x. "2015-07-16", you get "2015-07-16 12:55:00" or "1436187300".
Try debug the value of new Timestamp(fromDate.getTime()) first and if I'm right, try to convert it to a simple date without time.
To getting correct date value without time you can use:
Java 8 package java.time LocalDate https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
or lib Joda Time http://joda-time.sf.net/

Related

Find output of MySQL inbuilt function in CLI using custom parameter

I want to see the working of MySQL functions FROM_UNIXTIME() and UNIX_TIMESTAMP() by providing the parameters to them myself in CLI, something like this:
SELECT UNIX_TIMESTAMP(1459460268);
without having to insert these custom values in a table first and then selecting them to see the output.
Thank you for your time :)
UNIX_TIMESTAMP function accepts a date and you are giving it UNIX TIMESTAMP. You can simply run this in MySQL CLI and get the results:
SELECT UNIX_TIMESTAMP("2016-04-01 03:07:48");
SELECT FROM_UNIXTIME(1459460268);
SELECT UNIX_TIMESTAMP();
The last one will return you the current UNIX TIMESTAMP.
Happy coding!

converting java time to sqldate in query

java datetime (date.getTime()) is stored as string in mysql field.
How can we convert this to sql date using sql query. I am using mysql database.
Is there any sql function available?
For example - This is stored (1416231812348) for today's date in db.
Thanks for suggestions.
Java is returning the date as a long, to convert it you can use:
SELECT FROM_UNIXTIME(event_time) FROM MY_TABLE
If you get an error, try the following (after testing, I can see that your data is stored in milliseconds so you need to use this method):
SELECT FROM_UNIXTIME(event_time/1000) FROM MY_TABLE
(Change event_time to be the field name in your table and MY_TABLE to be the table name.)
Here is a SQLFiddle example that shows it working.
Here is an answer that gives you formatting options as well:
http://notsoyellowstickies.blogspot.co.uk/2011/11/converting-long-into-datetime-mysql.html
There is a java.sql package, that has time included. You can send it straight into your database without needing to convert it.
This may be a more pre-emptive solution than converting a date string from Java, into time in MySQL.
A similar question was answered and may be able to help you out here:
A datetime equivalent in java.sql ? (is there a java.sql.datetime ?)
most probably you have recorded from:
System.currentTimeMillis()
so:
select DATE_FORMAT ( from_unixtime( your_table_field / 1000 ) , '%e %b %Y');
you can change the date format as you like.

Getting no output while passing String parameters in mysql query

In the given below these two queries the first query runs successfully and display the result but while running second query i am getting no output and no error.
So can any please explain why i am not getting output in second query.
String firstdate="2014-11-01";
String lastdate="2014-11-30";
//1 query
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN '2014-11-01' AND '2014-11-30' AND
reader_id="+Integer.parseInt("136"));
//2 query
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN "+firstdate+" AND "+lastdate+" AND
reader_id="+Integer.parseInt("136"));
Well when you doing second query and giving external parameters u need to do something like this.
getschedule=con.getreader_schedule("SELECT startTime,endtime,available,comments FROM
reader_available_time WHERE startTime BETWEEN '"+firstdate+"' AND '"+lastdate+"' AND
reader_id="+Integer.parseInt("136"));
tihs is what i think its should work.
Ideally, you should be using prepared statements in your queries, to avoid this kind of issues. For a tutorial on prepared statements(using java), check here
To solve your existing query,
You are not getting output in the second query because, on substitution of the variables, your query would be:
WHERE STARTTIME BETWEEN 2014-11-01 AND 2014-11-30
which is wrong and bound to throw exceptions.
Correct query should be
WHERE STARTTIME BETWEEN '2014-11-01' AND '2014-11-30'
which implies, you should change your text to
WHERE STARTTIME BETWEEN '"+firstdate+"' AND '"+lastdate+"'

Hive's hour() function returns 12 hour clock value

According to the documentation Hives standard function hour() should return a value between 0 and 24 but for some reason I always get a twelve hour clock value, between 0 and 12. I'm using a MySQLDateTime field as a Timestamp field in my Hive table. Anyone know what the problem might be?
I think I found it. I looked at the source code and apparently UDFHour.java does have two evaluate() functions. One that does accept a Text object as parameter and one that uses a TimeStampWritable object as parameter. Both work with a Calendar instance but for some reason the first function returns the value of Calendar.HOUR_OF_DAY and the second one Calendar.HOUR.
I've looked in the Hives documentation but I couldn't find anything about that second function, but it's there. I'm using Hive 0.9.0.16, which came with Hortonworks' HDP.
Edit:
I've reported this a while back. A patch is now available: https://issues.apache.org/jira/browse/HIVE-3850.
Regardless of what hive has done, you could format the date to be returned as 24 hour format.
select FROM_UNIXTIME(mydate)
from mytable
;
Or you may update all datetimes stamps if it makes sense.
Reference
The function hour() returns a 24h formatted result if it works with string format. You may use
hour(cast (column_name as string))
for lower version of hive,I got a workaround
hour(from_unixtime(
unix_timestamp(
from_utc_timestamp(
from_unixtime(round(created_at/1000)),'Etc/GMT-8')
)))
Since I am using EMR, I can not choose to use the latest version of hive, so I got this workaround.
Just to show an example for what has already been told above
HOUR(cast (from_utc_timestamp(my_date_timestamp ,'GMT') as string)) -- returns 24 hr format
HOUR( from_utc_timestamp(my_date_timestamp ,'GMT') ) --returns 12 hr format

JPA hibernate date between query issue

In my application am using JPA entity manager to persist data/fetch data.
em.executeQuery("select * from file_calender_mapping where start_date between :start and :end");
em.setParameter("start",startDate)//startDate is an date object
em.setParameter("end",endDate)//endDate is an date object
List fmlist=em.execute();
The proble is just like this,
"select * from file_calender_mapping where start_date between start and end"
when am passing some date as start= "2011-08-03 05:08:00",and end="2011-08-04 06:08:00"
then the mysql return one row having the start time ="2011-08-03 05:30:00",its good,But
when my application executing such query it dose not returning any row.Actually what i have seen that my application returning value for two different date,but not for same date different time,thats the main problem.
One another thing is my "start" field for Table "file_calender_mapping" datatype is "timestamp".
So what i was thinking that ther may be some problem on JPA/Hibernate
You can try to specify the exact types of parameters as follows:
em.setParameter("start", startDate, TemporalType.TIMESTAMP);
em.setParameter("end",endDate, TemporalType.TIMESTAMP);
I have the strong feeling that you're confusing EntityManager.createQuery() with EntityManager.createNativeQuery() and you're somehow capturing all the exceptions, which somehow makes you don't receive anything back.
I'm assuming that, because I don't think you have a class named file_calender_mapping.
edit
The documentation will explain it better than I do, but a JPA QL query is transformed to the navite sql of the DB using the mapping, while a native query is send as it's to the DB.
Again, I suggest you to read the documentation, it's quite useful.