This might be a re-post, but I haven't been able to find anything that answers the question for me. I have a table with days listed as doubles like 435.6 or 5.2 and I need to convert this into a month. I've tried:
SELECT day FROM table WHERE DATE_ADD('2014-01-01', INTERVAL 31 DAY);
But that query just spits out the same. Any help please.
edited version:
Assuming your table is named t and your decimal column is named d
select date_format(date_add('2014-01-01', interval d day),"%m") from t;
This should add your epoch date ('2014-01-01') and format the result as a number (00-12), after adding d days to the epoch. For the month represented as a word (January-December), use "%M". here's documentation for DATE_FORMAT.
here is a fiddle of the above code working for sample values
Related
Getting an error when trying to correct the year portion of imported dates.
CSV Date Column Values were formatted
07/21/18 instead of
07/21/2018
This caused MySql to Insert Date as 07/21/0018
I was under the impression that year values in the range 00-69 were converted to 2000-2069 as stated in the documentation.
Any way to fix this? I've tried quite a few statements with no luck...
Any help appreciated
Assuming you want to just update the data in place, and it is a column of Date, DateTime or Timestamp types, you could do this:
UPDATE table SET date = date + INTERVAL 2000 YEAR WHERE YEAR(date) < 70
I have a data table whose structure looks like this.
date time order_id action quantity
How can I query to know how many entries are made to this table previous minute?
Say suppose the time now is 14:46 I want to know how many row entries were made to this table at 14:45. how can I do that?
The problem I am facing now is that I don't know how can I get last minute's time stamp. CURRENT_TIMESTAMP() is giving me correct current time. But I tried CURRENT_TIMESTAMP()-1 which gives some decimal number.
you can use DATE_SUB(), specifying a date and an INTERVAL as documented for DATE_ADD(). Example:
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);
You can use my query to find the last minute :
SELECT DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 MINUTE);
I have a column with timestamp, contain example value "2014-04-16 18:00:00","2014-04-17 18:00:00"....
Now, if I will call a page before "2014-04-17 12:00:00" I need this value-"2014-04-16 18:00:00"
And if I call my page after "2014-04-17 12:00:00" I need this value "2014-04-17 18:00:00".
I think my question is very complicated to understand, having complications in date & times, please check date & time properly.
I want to fetch this data from DB in mysql, The page I was saying is that where I'm going to add your mysql query.
Thanx in advance
Generalising what your asking for a bit the following will return dates from the previous day if it's before noon and dates from today if it's after noon:
SELECT date_column
FROM yourTable
WHERE DATE(DATE_SUB(NOW(), INTERVAL 12 HOUR)) = DATE(date_column);
Edit:
The WHERE clause First gets the current time (NOW()) and subtracts 12 hours. This wont affect the date unless the time is before 12. This means DATE_SUB(NOW(), INTERVAL 12 HOUR) gives us today if it's after noon and yesterday if it's before.
We then check if the date_column matches the date we've created (using the DATE function so that the time is ignored).
Adding some rows to the SELECT may help you see how these dates are built up.
I have a field in my table called created_date. The date format is 2010-02-28. I just wondering is it possible to do a mysql statement, only return the day instead of the entire date. eg. 28
SELECT
day(created_date)
FROM
table
This above query throw me error, is there a way i can do similar stuff?
cheers
Use MySQL built-in function called DAYOFMONTH
mysql> SELECT DAYOFMONTH('2007-02-03');
-> 3
DAYOFMONTH()
From Docs,
Returns the day of the month for date, in the range 1 to 31, or 0 for
dates such as '0000-00-00' or '2008-00-00' that have a zero day part.
MySql EXTRACT function extracts day, month or year from a given date.
select extract(day from created_date) as created_day from table
you can fetch the whole date in your format and display only the required field that is date by using this
date("j", strtotime(date('Y-m-d')) );
first of all, I know that my question is very similar to that one:
MySQL select rows from exactly 7 days ago
the difference is that my dates are stored in the database as a timestamp.
I know that I can use FROM_UNIXTIME to get the date from the timestamp, the thing is, in another answer I read that was very resource consuming (because the timestamp field has to be converted to date in all the records before comparing).
DATE(from_unixtime(timestamp)) = CURRENT_DATE()
Is there any optimized way to do this?
Turn it around: calculate the unix timestamp of the target date first and use that.
WHERE timestamp = UNIX_TIMESTAMP(NOW() - INTERVAL 7 DAY)
MySQL should calculate that value once and use it all the time (needs testing though). If it doesn't, use a variable or code.