I have a field receiveddate which is a datetime data type. I am trying to return results 7 days back from that date. So basically I want to see anything with a received date of today back 7 days. I tried several things including
c.ReceivedDate BETWEEN DATEADD(dd,-7, GETDATE()) AND getdate() but its pulling results from 2011! Im using SQL Server 2008. Any suggestions?
you code should be
c.ReceivedDate BETWEEN (GETDATE() - 7) AND GETDATE()
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 want to retrieve data between two specific date and I am getting that perfectly right with this query
select * from POS.dbo.voucher where date_time between '10 october 2014 00.00.00 ' and '11 october 2014 12.00.00'
but the issue is if I'm changing the month something like
select * from POS.dbo.voucher where date_time between '10 march 2014 00.00.00 ' and '11 march 2014 12.00.00'
It's still returning me with the same records. Seems like its only comparing the date and returning me the records.
Any idea where i m doing it wrong?
I am taking date as varchar in database.
Thanks!!
You appear to be using SQL Server (based on the dbo). Use a standard date format for date constants:
select *
from POS.dbo.voucher
where date_time between '2014-10-10 00:00:00' and '2014-10-11 12:00:00'
However, for this to work, you need to store dates using proper date formats in the database. So, you should fix the data structure to store dates using the correct data type.
I agree you would be better off changing the field from varchar to datetime. If that's not an option then you should convert the varchar field in the query into a datetime as appropriate in MS SQL Server, probably through a function, perhaps through a cast. BTW, best to tag with actually db used, and maybe SQL too.
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 system that auto-populates a mysql database with data. Unfortunately the the only time value is being stored in the format YYYYMMDDHHMMSS.SSS. Have a script that runs against the data in the database in a 30 minute cron to alert on certain data trends. The problem that I'm having is that the data in the database goes back 7 days. I only want the script to run against the past 30 minutes of data.
I tried using something like:
SELECT foo FROM table WHERE StartTime >= DATE_SUB(NOW(), INTERVAL 30 MINUTES);
Unfortunately this didn't work, I'm assuming because the stored time formation does not match a standard SQL Timestamp format.
Can anyone suggest an alternate method? Thanks.
StartTime >= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 30 MINUTE),'%Y%m%d%H%i%s.000')
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.