Float to date in mysql - 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

Related

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"

WHERE AND WHERE NOT in same query mysql

Is there a way to use WHERE NOT and WHERE in the same query, I am writing a python script where a function queries a mysql database, I need the script to call a specific name or have it return everyone, and on a side note if I can have it call groups of names that would be even better. I am accomplishing the name query with a simple LIKE statement.
SELECT * FROM db.table WHERE column LIKE '%searchterm';
However I want to be able to sort the data further using its DATE column, the query I have written for DATE is,
SELECT * FROM db.table WHERE NOT (column < startdate OR column > enddate);
Both queries work as intended when run on their own, when I try to combine them like so however I get a syntax error.
SELECT * FROM db.table WHERE NOT (column < startdate OR column > enddate) AND WHERE column LIKE '%searchterm';
Is there a way to fix the query so that it works?
You have one to many where in your query try changing to
SELECT * FROM db.table WHERE NOT (column < startdate OR column > enddate) AND column LIKE '%searchterm';

Trunc date field in mysql like Oracle

I am to not able to use the 'trunc(in oracle)' function in 'mysql' database. I have a table called dlb_cc_purchase and date field called due_date in my 'mysql' database. The data displaying in the date field like 20-11-2014 00:00:00 (20-nov-2014). in oracle we are using query
select * from dlbcc_purchase where trunc(due_date) = '20-nov-2014'
Oracle DB will fetch the row with due date 20-11-2014 00:00:00. How can I use this function in 'mysql'?
I know this is a basic question, but i was trying to do this for long time with truncate, str_to_date... but not able to fetch value. Please help.
Use DATE(expr) function. Query example:
SELECT *
FROM dlbcc_purchase
WHERE DATE(due_date) = '2014-11-20'
You can use DATE_FORMAT().
example:
select * from dlbcc_purchase where DATE_FORMAT(due_date,'%d-%b-%Y') = '20-nov-2014'

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.

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