I have a UNIX-type timestamp stored in an INT column in MySQL. What is the proper way to retrieve this as a MySQL DATETIME?
(I found the answer when re-scanning the MySQL Date functions, but didn't see the answer on SO. Figured it should be here.)
FROM_UNIXTIME()
select from_unixtime(column,'%Y-%m-%d') from myTable;
SELECT FROM_UNIXTIME(mycolumn)
FROM mytable
The function STR_TO_DATE(COLUMN, '%input_format') can do it, you only have to specify the input format.
Example : to convert p052011
SELECT STR_TO_DATE('p052011','p%m%Y') FROM your_table;
The result : 2011-05-00
This works for sure.
select from_unixtime(column) from table
Related
I have looked through questions here and have not been able to find exactly what I am looking for.
How would I create a query in mysql that would return missing data in field Name in database Demo as a string (such as 'Blank') instead of NULL?
I really appreciate your help!
SELECT IFNULL(fieldname, 'Blank') FROM tablename
or
SELECT COALESCE(fieldname, 'Blank') FROM tablename
Yes, in MySQL you can use IFNULL, as in:
SELECT IFNULL(fieldname, 'Blank') as fieldname, ...
I'm not really sure what you're aiming at, but maybe you look for something like this?
SELECT IF(ISNULL(a.yourfield),'Blank', a.yourfield)
FROM yourtable A;
ISNULL() returns 1 if the argument is NULL, otherwise it returns 0.
How can I convert a string/varchar like '12-Mar-2013' to date like 2013-03-12?
I tried
SELECT STR_TO_DATE('12-Mar-2013','%Y-%m-%d');
SELECT DATE_FORMAT('12-Mar-2013','%Y-%m-%d');
but both return null.
My current database version is 5.5.7-rc.
Use STR_TO_DATE.
Try
SELECT STR_TO_DATE('12-Mar-2013','%d-%M-%Y');
-> 2013-03-12
This should do the trick
SELECT DATE_FORMAT(STR_TO_DATE('12-Mar-2013','%d-%b-%Y'), '%Y-%d-%m');
I have two strings "0.31" and "0.0076" and they need to be stored in a decimal(10,2) column in MySQL. How do I do this conversion in ruby but not in mysql directly
try using CAST
SELECT CAST(colName AS DECIMAL(10,2))
FROM tableName
SQLFiddle Demo
use MySql conversion functions CAST or CONVERT . Read Here
Select CAST(columnName as DECIMAL(10,2))
or
Select CONVERT(columnName,DECIMAL(10,2))
OR you can do the job like
select format(columnname, 0) as formated from tablename where condition
I want to query data in mysql. The problem is the column is datetime(dpost) and i will use a date in the where clause.
SELECT * FROM `uid1000_qposts` WHERE `dpost`="2011-12-06"
this query doesn't return any results. Is there any possible way I can query data using date against a datetime column?
thanks
SELECT * FROM `uid1000_qposts` WHERE date(`dpost`) = '2011-12-06'
Try this:
SELECT * FROM `uid1000_qposts` WHERE DATE_FORMAT(dpost,'%Y-%m-%d')='2011-12-06'
WHERE dpost between "2011-12-06 00:00:00" and "2011-12-06 23:59:59"
Another possibilities is using
DATE_FORMAT(dpost,'%Y-%m-%d')="2011-12-06"
but is not recommended,
as it won't make use on any index (even there is)
SELECT * FROM `uid1000_qposts` WHERE date(`dpost`) = '2011-12-06'
Is there an easy (single query) way to do this?
I'm reading those values from a column in a table and I think that the column itself is defined as a string (can't be helped, i'm afraid).
Use UNIX_TIMESTAMP;
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
Update:
SELECT UNIX_TIMESTAMP(CAST(fieldName AS DATE));
SELECT UNIX_TIMESTAMP(STR_TO_DATE('08/05/10','%m/%d/%y'));
SELECT '12/31/10',
STR_TO_DATE('12/31/10', '%m/%d/%y'),
UNIX_TIMESTAMP(STR_TO_DATE('12/31/10', '%m/%d/%y'))
Both functions are covered here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html