Converting date from the column to the unix_timestamp - mysql

I got date, stored in MySQL column like this: 09-11-2012 (format is DD/MM/YYYY), but when I do:
SELECT UNIX_TIMESTAMP(t.date) FROM table t;
It does returns 0 for every single row.
How can I convert this format date to the unix timestamp? I would like to convert this date just like the PHP's strtotime is doing it.

I guest t.date is a string right? try converting it to DATE first using STR_TO_DATE
SELECT UNIX_TIMESTAMP(STR_TO_DATE(t.date, '%d-%m-%Y'))
FROM table t;
SQLFiddle Demo

Related

How to sort column in mysql having date in format of %m/%d/%Y?

I want to sort a date-of-birth column in a table which has date stored in format of %m/%d/%Y . When I use order by , the column values are being treated and sorted as string and not date. I have tried UNIX_TIMESTAMP() function but it also seems to be not working. What are the possible solutions ?
Use STR_TO_DATE() Function
SELECT * FROM yourtable
ORDER BY STR_TO_DATE(DueDate,'%m/%d/%Y ') DESC

str_to_date returns null+mysql

When I am trying to convert varchar to date, I get Null values in return.
I have values as 05-MAR-2015 in my column.
I am running following query.
select STR_TO_DATE('n.Invoice_date','%d-%c-Y') from table n;
Once I run above query I get null values in return.
I want to introduce a new column in date format.
Note that the literal string 'n.invoice_date' is not a valid date. What you mean is:
SELECT STR_TO_DATE(n.invoice_date, '%d-%b-%Y') FROM TABLE n
Your error is in usage of %c instead of %b to get the date. You mixed the formatting of the date with the creation of a date value. This should do it:
SELECT DATE_FORMAT(STR_TO_DATE(n.invoice_date,'%d-%b-%Y'), '%d-%c-%Y') FROM table n;
This results in: 05-3-2015
Here you first create a date with STR_TO_DATE which must match the format in which it is stored in the field. Then you can format it with DATE_FORMAT in the way you want to.
See the MYSQL Docs for more information: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

mysql DATE_FORMAT() between gives me bug

I am trying folowing on my_table where modifiedtime is of type datetime
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where DATE_FORMAT(modifiedtime,'%d-%m-%Y') between '05-11-2013' and '28-11-2013';
The query gives me some other record too which are not falls between above dates, for example there is a record in result set dated '04-01-2014'
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where DATE_FORMAT(modifiedtime,'%d-%m-%Y')='05-11-2013'
this query works fine and gives all the records for the given date
why the first behaves like that?
How can i correct it?
what is the efficient way to implement it?
such that i can get all the records only between given two dates.
SELECT
DATE_FORMAT(modifiedtime, '%d-%m-%Y')
FROM
my_table
WHERE
modifiedtime BETWEEN STR_TO_DATE('05-11-2013', '%d-%m-%Y') AND STR_TO_DATE('28-11-2013', '%d-%m-%Y');
DATE_FORMAT() returns TEXT type column and dates can't be applied.
Use without DATE_FORMAT in the WHERE
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where modifiedtime between '05-11-2013' and '28-11-2013';
you DATE_FORMAT function converts the column modifiedtime to String.
and hence in your first query you do a string comparison rather then a date comparison.
Also your date literal is not incorrect. It must be of form YYYY-MM-DD
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where cast(modifiedtime as date) between '2013-11-05' and '2013-11-28';

sorting unix type date in mysql

Is there any way to retrieve SORTED resultset from mysql table where date is stored in unix way? I mean something like this "Select * from tableName order by DATE DESC", if its in Unix type it means it is stored as integer or bigint, so it doesnt really work that way I wrote here, any help?
You can use FROM_UNIXTIME.
SELECT ..
ORDER BY FROM_UNIXTIME(column)
Documentation
If you want to order the data by date instead of the unix int/bigint then you can convert the unix time to a date using FROM_UNIXTIME
select *
from tableName
order by FROM_UNIXTIME(DATE) desc
Ordering by the unix value before the conversion should still work because it is an int value.

Change MYSQL date format

I have a table in MYSQL of which 3 columns have dates and they are formatted in the not desired way.
Currently I have: mm/dd/yyyy and I want to change those dates into dd/mm/yyyy.
Table name is Vehicles and the columns names are:
CRTD
INSR
SERD
Your current datatype for your column is not date right? you need to convert it to date first using STR_TO_DATE() and convert back to string
SELECT DATE_FORMAT(STR_TO_DATE(colName, '%c/%d/%Y'), '%d/%c/%Y')
FROM table1
SQLFiddle Demo
try this:
select date_format(curdate(), '%d/%m/%Y');
In you case you have to use this query.If all three columns are of datetime type
select date_format(CRTD, '%d/%m/%Y'),
date_format(INSR, '%d/%m/%Y'),
date_format(SERD, '%d/%m/%Y')
From yourTable
You can use the date_format() function to format your dates any way you want.
If you want to change the format for every date on any database you work with, you should change the value of the date_format system variable.
UPDATE Vehicles SET yourdatecolumn=DATE_FORMAT(STR_TO_DATE(yourdatecolumn, '%c-%d-%Y'), '%d-%c-%Y')