I have a database where all my data have a unix timestamp as a integer, the integer is the amount of seconds since 1.jan 1970.(like what Time.now.to_i returns in ruby http://www.unixtimestamp.com).
Is there any way i can get the date from 1447277423 in SQL? I need to group the rows by date.
I want this to be done in a view, and not use another script to do it.
Is this possible?
Convert the unix timestamp to date only (since you don't want hours/seconds), then group by it.
SELECT * FROM table
GROUP BY FROM_UNIXTIME(date_timestamp, '%Y %m %d');
Or, if you don't want to actually group them and just want them outputted all in order, ORDER BY instead.
SELECT * FROM table
ORDER BY date_timestamp
/* when ordering, it doesn't matter so much if its the whole timestamp or not since the date comes first in the timestamp */
To go one further, you can SELECT the formatted date as well
SELECT *, FROM_UNIXTIME(date_timestamp, '%Y %m %d') as date_Ymd FROM table
ORDER BY date_timestamp;
FROM_UNIXTIME docs
Just use FROM_UNIXTIME():
group by DATE(FROM_UNIXTIME(your_unix_timestamp_field))
That is the nice part of unix timestamps, you can just treat it as an integer.
SELECT something FROM table WHERE date >= 1447277423
Hope this helps
Related
I have a column where a date store in ddmmyy format (e.g. 151216). How can I convert it to yyyy-mm-dd format (e.g 2016-12-15) for calculating a date difference from the current date? I try using DATE_FORMAT function but its not appropriate for this.
If you want to get the date difference, you can use to_days() after converting the string to a date using str_to_date():
select to_days(curdate()) - to_days(str_to_date(col, '%d%m%y'))
or datediff():
select datediff(curdate(), str_to_date(col, '%d%m%y'))
or timestampdiff():
select timestampdiff(day, str_to_date(col, '%d%m%y'), curdate())
You can use the function, STR_TO_DATE() for this.
STR_TO_DATE('151216', '%d%m%y')
A query would look something like:
select
foo.bar
from
foo
where
STR_TO_DATE(foo.baz, '%d%m%y') < CURDATE()
Note: Since both STR_TO_DATE() and CURDATE() return date objects, there's no reason to change the actual display format of the date. For this function, we just need to format it. If you wanted to display it in your query, you could use something like
DATE_FORMAT(STR_TO_DATE(foo.baz, '%d%m%y'), '%Y-%m-%d')
To get the difference, we can simply subtract time
select
to_days(CURDATE() - STR_TO_DATE(foo.baz, '%d%m%y')) as diff
from
foo
If you wanted to only select rows that have a difference of a specified amount, you can put the whole to_days(...) bit in your where clause.
SELECT STR_TO_DATE('151216', '%d%m%y') FROM `table`
use this '%d%m%y'
I want to use a Datepicker to select specific entries in my Database.
The Problem i face is that the entries i get as a reposnse from my query are only sorted for the day part of the date.
I use the date format (varchar field in database):
*dd.mm.yyyy hh:mm ($date = date("Y-m-d") . ' ' . date("H:i");)*
For example:
01.04.2016
10.04.2016
11.04.2016
01.04.2017
10.04.2017
11.04.2017
The Query to get the entries:
SELECT *
FROM order_date
WHERE date >= '10.04.2017' AND date <= '10.04.2017'
Now i expected to get only the to matching entries but
the result of the Query are all four table entries which start with day 10 or 11. Even two of them are in the year 2016.
As i saw in other posts the between XX and YY work if the date is set with yyyy-mm-dd or yyyymmdd but the problem is that i got a database with around 20k entries on which i need a datepicker so i would prefer to use the givin format of the date without rewriting all entries.
Is there a possibilty to get a datepicker between with dd.mm.yyyy format aswell?
Any help highly appriciated
Try something like this,
STR_TO_DATE('10.04.2017', '%d.%m.%Y')
SELECT *
FROM order_date
WHERE date >= STR_TO_DATE('10.04.2017', '%d.%m.%Y') AND date <= STR_TO_DATE('10.04.2017', '%d.%m.%Y')
You can typecast the column as DATE, and use the ORDER BY clause.
Use STR_TO_DATE function may help.
STR_TO_DATE('10.04.2017','%d.%m.%Y')
How about something around this idea ?
SELECT *
FROM Mytable WHERE date
BETWEEN CONVERT(datetime,'10.04.2017',104 )
AND CONVERT(datetime,'10.04.2017',104 )
I have a column that stores dates as text, I need to select all the entries with date less than the date of today.
If I use this:
SELECT *
FROM mytab
WHERE expire < CURRENT_DATE( )
ORDER BY expire DESC
It doesn't select the correct entries but only the ones with da_expire empty.
How can I fix it?
In the first place, why are you storing it as string?
You need to convert it to date using MySQL's builtin function so you can be able to compare it with today's date.
WHERE STR_TO_DATE(expire, '%Y/%m/%d %H:%i') < CURDATE()
This will be a little slower since it will not use any index if you have one defined on the column.
MySQL Docs: STR_TO_DATE()
Use STR_TO_DATE(expire, '%m/%d/%Y') instead of expire in the query. I have assumed you are storing the date in month day year format. You will need to adjust the format as per the string format. However, for performance reasons convert the type of expire during load/insert process .
I try to order the results of a query by the date which is in the format yyyy/mm/dd and I am using this query to no avail.
SELECT * FROM table ORDER BY STR_TO_DATE(date, '%y/%m/%d')
I can't change the type of field that the date is stored in so I am hoping I can order the date post data entry.
Any help and advice appreciated.
If the date is in that format, you don't need to convert it to a date, surely? That format would sort alphabetically, assuming it is 0 padded... (i.e. July is 3 July 2012 is 2012/07/03...)
So you can just go:
select * from table order by date
What type of field is your date field: are you sure it is a varchar?
Assuming it is a varchar, you can work out what is going wrong by going:
select str_to_date(date, '%y/%m/%d') from table
You (should) get all NULL's, because the %y is wrong. Try:
select str_to_date(date, '%Y/%m/%d') from table
and it should work. But as noted, you don't have to convert to sort.
Try with capital Y:
SELECT * FROM table ORDER BY STR_TO_DATE(date, '%Y/%m/%d')
Lowercase Y is for yy, uppercase for yyyy.
SELECT * FROM table ORDER BY date
I want to select rows from a table given a particular date of record in mysql
SELECT * from TABLENAME WHERE FROM_DATE='06/11/2012'
I am not getting anything useful.
First of all, you should use the standard date format Y-m-d - otherwise you have to make some nasty queries and sorting is a real b*tch.
Using the standard date format you can easily do something like this:
SELECT * FROM tablename WHERE from_date > '2012-06-11'
DATE comparisons are very likely what you want here. If your from_date column has the data type of DATE, then your code should be safe and robust if you do this:
WHERE from_date = STR_TO_DATE('06/11/2012', '%m/%d/%Y')
#Repox pointed out that you might consider putting your date literals in the canonical format '2012-06-11'. That's true, if you can do it. But STR_TO_DATE will do it for you if you need it to. There's a list of the %x conversion items here. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
If you're using DATETIME data types, beware: comparisons are more complex than they seem. DATETIME items are like floating point numbers: if one of them exactly equals another it's only by coincidence. That's because they represent moments (milliseconds) in time, not just days.
Presuming your from_date column has the DATETIME type, you should use
WHERE from_date >= STR_TO_DATE('06/11/2012', '%m/%d/%Y')
AND from_date < STR_TO_DATE('06/11/2012', '%m/%d/%Y') + INTERVAL 1 DAY
This will catch all moments in time on the day you want, up to but not including the first moment of the next day.
If your from_date items are represented as character strings, take the trouble to convert them to DATE or DATETIME data types. Seriously. Your results will be far better.
SELECT * from TABLENAME WHERE FROM_DATE='2012/06/13'
It would be better if you use the DATE() function of mysql
SELECT * FROM tablename WHERE DATE(from_date) > '2012-06-11'
Because, if the datatype of the from_date you set as TIMESTAMP or DATETIME then it won't return the correct results sometimes when you directly use the '>' symbol