Turning Off Timestamp Formatting in MySQL - 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

Related

show createdAT timestamp according to timezone rather that utc

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'

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"

Mysql - How to compare date (mm/dd/yyyy) with another another date from database?

This is my SELECT query
season_from_date >= "06/09/2015" and season_to_date <=
"06/11/2015"
The same date format(mm/dd/yyyy), am saving in my database. I am not getting error but its not showing any result? Is that correct method or do we have any other method?
You should never store in those format and using varchar field, you should always store date data with mysql native datatypes this makes life easy. However in this case you may use str_to_date function
select * from table_name
where
str_to_date(season_from_date,'%m/%d/%Y') between
str_to_date('06/09/2015','%m/%d/%Y')
and
str_to_date('06/11/2015','%m/%d/%Y')

Filter dates stored as varchar in mysql

I am working with a MySQL database where dates are stored as varchar like this:
'2013-01-31' in column cl_223
I need to select only records from 2013 so I tried:
SELECT ..
FROM ....
Where cl_223 Like '2013'
But that does not seem to work.
Thanks for all help!
You must add % as a wildcard :
SELECT ..
FROM ....
WHERE cl_223 LIKE '2013%'
Storing a datettime value in a varchar column complicates some functionality on date time operations. But of course you can select your values writing such a query as follow
SELECT * FROM table_name WHERE cl_223 LIKE '2013%'
But if you don't have any performance issue you can convert the varchar column to a datetime value and write stronger typed query like this:
SELECT * FROM table_name WHERE STR_TO_DATE(cl_223,'%Y-%m-%d2') BETWEEN '2013-01-01' AND '2013-12-31'
But if you need a date time value as a date time in your process you'd better store it in a datetime column instead of a varchar column.
The query should be
SELECT ..
FROM ....
Where cl_223 Like '2013%'
However, the better solution would be to store the dates as DATE data types. If the dates in that column are always used in the format they're in now, the change would be backwards compatible. It would also allow for easier processing of the date values.

Float to date in mysql

We have a script that saves some rows in a table and one of them is filled with Time.now.to_f. So we have values like:
1330017921.1065
1330018520.80347
Is there a way to convert this to a date directly in MySQL I now I can use Time.at but I need a row mysql query.
i guess this is a microtime.
if you don't need milliseconds, you could query something like
SELECT * FROM table WHERE datefield = FROM_UNIXTIME(rounded_unixtime)
where rounded_unixtime would be in your case 1330017921