What is the current date in New York? - mysql

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';

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

MySQL says 'SYSDATETIME' does not exist

I am trying to select rows from a table where the date column value is equal to the current date in Sydney, Australia (UTC+10h). The server is based in Sydney, and so I would like to use SYSDATETIME(). This is my query:
SELECT * FROM database WHERE DATE(date) = DATE(SYSDATETIME())
But this returns the error:
1305 - FUNCTION database.SYSDATETIME does not exist
What am I doing wrong?
EDIT: I used this documentation
As the error message suggests, there is no such function as SYSDATETIME in MySQL. The documentation you are referring to is for Microsoft SQL Server.
The function you are looking for is simply called NOW().
You have to use SYSDATE instead of SYSDATETIME.
The SYSDATE function will return the current date as a 'YYYY-MM-DD HH:MM:SS' format.

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".

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.