show createdAT timestamp according to timezone rather that utc - mysql

I am putting following query to see the records:
select * from dbName.TableName to get the records .
now what happens is it shows me createdAt' andupdatedAt' in utc format. So is there a way through which , I can see it in IST (indian standard time) format while querying ?

You need Select_convert_tz() function of MySql. Have a look at documentation
Edit based on comment:
query should look like
to select converted timezone
select select_convert_tz(column,'from_timezone','to_timezone') from table where condition
or to query based on timezon
select * from table where select_convert_tz(column,'from_timezone','to_timezone')>'some value'

Related

I have date stored as varchar in mysql database with format like that 07/17/2017

I have stored datetime as varchar unfortunetly i have lot of data comes in , now i want to get records between two dates below is my query but it is not returning the correct result set
$start_date=$_POST['start_date'];
$to_date =$_POST['end_date'];
$wbs =$_POST['wbs'];
if(!empty($_POST['wbs']))
{
$query="SELECT * FROM plan_voucher WHERE location=".$_SESSION['role']." and created_at between '".$start_date."' and '".$to_date."'";
use date conversion that already said #Tim in his comments and try your query like below way
SELECT * FROM plan_voucher WHERE location=".$_SESSION['role']."
and STR_TO_DATE(created_at,'%m/%d/%Y') between '".$start_date."'
and '".$to_date."'";

MySQL HOUR(TIMESTAMP) considering timezone

I have a table that contains a column of type TIMESTAMP (which is stored in UTC). My server is currently in timezone 'America/New_York'. I have the following query:
SELECT * FROM table1 WHERE hour(time) > 5;
Which returns the expected output. I then want to change the server's timezone to something else (doesn't matter what) and I want to get the same output as when it was 'America/New_York'. Basically, I want a query that returns all rows where the hour of time in timezone 'America/New_York' is greater than 5, regardless of what the current server timezone is.
I know I could use:
SELECT * FROM table1 WHERE hour(convert_tz(time, ##session.time_zone, 'America/New_York')) > 5;
But I don't want to use variables (##session.time_zone) because I want this SELECT to be included in a view and views don't support variables.
Is there a way to force a TIMESTAMP to be converted to a specific timezone regardless of the current server's timezone?
UPDATE: I found a way to create a function and then call that function from within the view:
create function getTimeZone() returns VARCHAR(50) DETERMINISTIC NO SQL return ##session.time_zone;
This works fine, but I feel like it's a bit of a hack.

Mysql date select from string

i've issue I try this query, do not return any rows. just 0 rows. Even tho there is data matching the request..
select * from repairshop_reservations where date = DATE_FORMAT("11/06/2017 20:00:00", '%d/%m/%Y %H:%i:%s"');
Currently my content of the selected table look like this
The data value of column Date is datetime
you could use str_to_date in this way you can control the proper formatting of the date when you don't use the standard mysql format
select * from repairshop_reservations
where date = str_to_date('11/06/2017 20:00:00', '%d/%m/%Y %H:%i:%s');
You are not inserting a column in your table, so you won't have to define a data type for it. That means that, you are not making changes to the conceptual scheme of your database.
Considering that your table is implemented correctly, the SQL query you would need to give you the desirable result would be:
SELECT * FROM repairshop_reservations
WHERE date = "11/06/2017 20:00:00";
You use the WHERE clause, to filter your record and get an output with a
specified condition. In plain English, what you want to do is:
Select and print for me, every column from the repairshop_reservations table, that has listed date as "11/06/2017 20:00:00"

How to Query between a date range in MySQL

I'm querying
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '02-04-2017'
and the result is null. How to fix this. But
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '30-03-2017'
giving me the correct result.
Note:- tempLog is the table name.
You should store dates in date format or atleast correctly formatted string (YYYY-MM-DD).
For now you can use str_to_date to convert the string to date and compare:
select *
from tempLog
where str_to_date(date, '%d-%m-%Y') between '2017-03-23' and '2017-04-02';
However note that this will hinder the optimizer from using index on the column if any.
The correct remedy of the situation is fixing the table structure.
According to the documentation, you're supposed to use this format when writing a date: 'YYYY-MM-DD' (although it says it may accept 'YYYYMMDD' or even YYYYMMDD in some contexts).

Turning Off Timestamp Formatting in MySQL

I have many timestamp columns that I need to use with mixed timezones. The users timezone is set in the language I'm using for the front-end so I need MySQL to return a unix timestamp from a select *. Is there a way to turn off the auto formatting when fetching data from MySQL timestamp columns?
YYYY-MM-DD HH:MM:SS is the default representation for timestamp columns in MySQL. I don't believe you can change that on a global level.
Two options:
Instead of doing a SELECT *, do SELECT *, UNIX_TIMESTAMP(your_timestamp_column) AS your_timestamp, which will add a Unix-formatted your_timestamp column to the results.
Make a view (CREATE VIEW) for each table that does the same thing, e.g.
CREATE VIEW your_view AS
SELECT *, UNIX_TIMESTAMP(your_timestamp_column) AS your_unix_timestamp
FROM your_table;
Then you can do SELECT * FROM your_view; and get your Unix timestamp without adding anything to your queries.
Reference: UNIX_TIMESTAMP
Yes.
You can wrap the filed in the UNIX_TIMESTAMP function like this:
select UNIX_TIMESTAMP(your_timestamp_column) from your_table;
For more information about this and other mysql data and time functions see:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp