One column in a table in my MySQL database stores date in Unix format.
I am trying to retrieve all rows where the date is later than a specific date. How do I write a query to do this? I want something like:
SELECT * FROM table_name WHERE date > "yyyy-mm-dd";
Thanks in advance.
use FROM_UNIXTIME
SELECT *
FROM table_name
WHERE DATE(FROM_UNIXTIME(date)) > "yyyy-mm-dd";
Related
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."'";
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'
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.
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.
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