Find output of MySQL inbuilt function in CLI using custom parameter - mysql

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!

Related

How to perform correct select over unixtimestamp in mysql

I have table with UNIX sql time stamp like 1615582447 in receivedOn.
I am not sure, how to work with this timestamp in case of some interval.
What is the correct way how to perform sql according to user timezone?
My current code is (for Europe/Prague timezone):
FROM_UNIXTIME(receivedOn) > ("2021-03-07T23:00:00.000Z") AND FROM_UNIXTIME(receivedOn) < ("2021-03-2021-03-08T22:59:59.999Z")
But this select return data outside interval.
What is the correct way or...better?
this can#t work in that form, as you have no correct mysql time to compare it with
SELECT FROM_UNIXTIME(1615582447 ) > CONVERT("2021-03-07T23:00:00.000Z", datETIME)
returns true or 1
you should always use mysql conform dates and time, to use in your query, and let the programming language convert it into the right form

What is the current date in New York?

How do I fix this query to select the current date, in YYYY-MM-DD format, in New York?
SELECT CURDATE(); // sometimes returns the current date in New York, sometimes does not
My reporting software does not allow me to use SET or other statements or configuration options. I can only execute a single SELECT query. Is it possible to get the correct answer in the format above?
select date(convert_tz(now(), ##time_zone, 'America/New_York'))
but verify that the mysql server has timezone information installed by doing:
select * from mysql.time_zone_name where Name='America/New_York';

SQL Query nvarchar to date

I am working on SAP HANA Studio and have tried to run SQL command that converts an entire column of field, nvarchar, into one of field, date.
My dates have format: dd-mon-yyyy (i.e '29-Mar-1997') with field nvarchar(11).
I have looked at previous questions and SQL command documentation (for functions like CAST, CONVERT, TO_DATE, STR_TO_DATE) and have not gotten a solution.
Typical errors I get are: Function not recognized, or, Error while parsing Service Date as DATE at function to_date().
Any suggestions?
Thanks
-Diana
Try TO_DATE():
select to_date(col, 'DD-MON-YYYY')
Obviously your database driver/layer in SAP HANA does not support all mySQL functions.
Please connect to your database directly (using command-line or a gui like HeidiSQL) and create a view in your database:
CREATE VIEW view_tablename AS
SELECT STR_TO_DATE(`Service Date`, '%d-%b-%Y') AS ServiceDateDt, * FROM tablename
Then use view_tablename instead of tablename in all your queries - because view_tablename has the additional date field "ServiceDateDt".

JOOQ how query for between dates

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/

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.