I have a varchar column called date_submitted and it's formatted like so:
03-06-2014 4:32 pm
02-14-2014 2:44 am
And so on...
I was wondering how I would get all of the dates from a certain date or from something like 20 days ago. This select statement is something along the lines of what I need.
SELECT date_submitted FROM myTable WHERE date_submitted < 02-14-2014 12:00 am
try this with DATE_SUB
SELECT date_submitted
FROM myTable
WHERE STR_TO_DATE(date_submitted, '%m-%d-%Y') > DATE_SUB(DATE_FORMAT((NOW(),'%m-%d-%Y'), INTERVAL 20 DAY)
You need to CONVERT your datetime column appropriately like this:
SELECT date_submitted FROM myTable
WHERE convert(datetime,date_submitted,100) < DATE_SUB(convert(datetime,'02-14-2014 12:00 am',100), INTERVAL 20 DAY)
You need something like this, or else you'll be comparing a string to a date, you won;t get good results
SELECT date_submitted
FROM myTable
WHERE
date_format(str_to_date(date_submitted,'%m-%d-%Y %h:%i %p'), '%Y-%m-%d %H:%i:%s')
>DATE_SUB(NOW(), INTERVAL 20 DAY);
Related
i am try to request date only yesterday but without success...
My query request.
SELECT registeredDaySell FROM new_sell WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY)
My date is organized this way.
16 September, 2017
Thanks for helping me out.
subdate(now(),1) will return yesterdays timestamp
The below code will select all rows with yesterday's timestamp from employee_login page
Select * FROM `employee_login` WHERE `dattime` <= subdate(now(),1) AND `dattime` > subdate(now(),2)
The below code will display yesterday's timestamp
Select subdate(now(),1) ,subdate(now(),2))
This will give
SELECT producFinalPrice
FROM new_sell
WHERE WEEK (date) = WEEK( current_date ) - 1
As #Gordon mentioned, you should consider storing your dates either in some date type column, or possibly as a UNIX timestamp (seconds since the epoch). A possible workaround here would be to use STR_TO_DATE to convert your string dates to bona fide dates on the fly.
SELECT
producFinalPrice
FROM new_sell
WHERE
STR_TO_DATE(date_col, '%d %M, %Y') = DATE_SUB(CURDATE(), INTERVAL 1 DAY)
This assumes that date_col is the name of the column in your table which contains string dates.
SELECT producFinalPrice FROM new_sell
WHERE where date >= DATEADD(day, -1, convert(date, GETDATE()))
and date < convert(date, GETDATE())
-1 equates to "today" minus 1 day. You can change that number to get the number of days that you want to go back if further than 1.
I want to get count of all assessments in past three months. So this should count for current and previous two months as separate
like Count1 50 for this month
Count2 100 for one month back
Count2 50 for two month back
SELECT Count(AssessmentID)
from AssessmentListing
WHERE (STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p') >=
DATE_FORMAT(NOW(), '%Y' ))
You should not be storing dates as strings. If you want the current month and two months back, then something like this:
SELECT DATE_FORMAT(STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p'), '%Y-%m') as yyyymm,
Count(*)
from AssessmentListing
WHERE STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p') >= DATE_SUB(DATE_SUB(CURDATE(), INTERVAL 1 - DAY(CURDATE()), INTERVAL 2 MONTH)
GROUP BY yyyymm;
I am not sure what your WHERE clause actually does. But comparing a string and a date is probably not what you really want.
Like #Jarlh mentioned in comment you can use Extract function to extract the month from the date column and group the result using that.
select EXTRACT(MONTH FROM AssessmentSubmittedDatetime) as _month,
Count(AssessmentID)
from AssessmentListing
WHERE (STR_TO_DATE(`AssessmentSubmittedDatetime`, '%d-%b-%Y %I:%i %p') >=
DATE_FORMAT(NOW(), '%Y' ))
group by _month
I need to find upcoming birthday from database table name users and column name u_bday where u_bday is in varchar format.
i tried this but not work for me help.
$sql=" SELECT `f_name`,`u_bday` FROM `users`
WHERE DATE_ADD(`u_bday`, INTERVAL YEAR(CURDATE())-YEAR(`u_bday`)+
IF(DAYOFYEAR(CURDATE()) > DAYOFYEAR(u_bday),1,0) YEAR)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)";
what should i do???
use this function to convert your varcher column to date format
str_to_date( fieldname, '%Y-%m-%d')
You can use date_diff function and query could be somthing like below
SELECT `f_name`,`u_bday` FROM `users`
WHERE DATE_DIFF(`u_bday`, now()) > 1
ORDER BY u_bday asc;
I have a column called "s_timestamp."
How can I return all the records that have the current day in the timestamp?
For example,
s_timestamp
2012-12-27 1:00:00
2012-12-27 2:00:00
2012-12-26 0:00:01
2012-12-20 0:00:02
2012-12-21 0:00:03
I would like the following output:
2012-12-27 1:00:00
2012-12-27 2:00:00
Let me know if this is unclear.
just use CURDATE(). eg
SELECT *
FROM tableName
WHERE DATE(s_timestamp) = CURDATE()
DATE()
CURDATE()
This may be more efficient than casting the timestamps to DATE, especially if you have an index on the timestamp column (which you should have):
SELECT *
FROM tableName
WHERE s_timestamp >= CURDATE()
or, if you want to exclude any future dates:
SELECT *
FROM tableName
WHERE s_timestamp >= CURDATE()
AND s_timestamp < DATE_ADD(CURDATE(), INTERVAL 1 DAY)
This works because, when a DATETIME or a TIMESTAMP is compared with a DATE, the DATE is, in effect, interpreted as having a time part of 0:00:00.
I would like to rows that have only been entered in the last 1 day.
I have a date column which stores YYYY-MM-DD, and I allow the user to send a date that they want to look at in this format yyymmdd how can I use this data to limit it to the previous day only?
I would imagine it is something to do with the BETWEEN keyword but I cant figure it out.
SELECT * from TABLE_NAME WHERE ROW_DATE BETWEEN '2011-03-20' AND '2011-03-21'
This query:
SELECT *
FROM mytable
WHERE mydate >= STR_TO_DATE('110321', '%y%m%d') - INTERVAL 1 DAY
AND mydate < STR_TO_DATE('110321', '%y%m%d')
will return all records for Mar 20, 2011
From the MySQL manual (here):
SELECT something FROM tbl_name WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= date_col;
SELECT * FROM YourTable WHERE date_column = DATE_ADD(CURDATE(), INTERVAL -1 DAY)
This returns all rows for today and yesterday.