MySQL query to select a certain day in the past? - mysql

I have a joomla website and I want to make a query that it could run with a cronjob every day. It would look for any articles 30 days old and delete the articles this specific day.
I can make a simple query like this:
SELECT * FROM `ibps6_content`
WHERE created > '2013-10-16' AND created < '2013-10-16'
But I don’t know how to make it specify the last 30 days, instead of hardcoding the dates.

Which rdbms?
In oracle you can use select based on sysdate which represents the current date/time.
Example:
select * from ibps6_content WHERE created between sysdate - 30 and sysdate

In T-SQL, you could use the following:
SELECT
*
FROM
ibps6_content
WHERE
created BETWEEN DATEADD(DAY, -30, GETDATE()) AND GETDATE()
Note that this will give you the articles written up to the specific time that you run the article. To remove the times and utilize just the dates, you could use:
DECLARE #CurDate DATETIME;
SET #CurDate = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()));
SELECT
*
FROM
ibps6_content
WHERE
created BETWEEN DATEADD(DAY, -30, #CurDate) AND #CurDate

If created is a date datatype in the table
DELETE FROM ibps6_content WHERE created = CURRENT_DATE -INTERVAL 30 DAY
If it's datetime use DATE(created)

You need mysql DATE_FORMAT(date,format) function.
Please try to set your query as:
DELETE FROM `ibps6_content` WHERE created > DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-%d');
Hope this helps

Related

How do i retrieve data from YTD to last sunday?

I'm trying to make an SQL code so it automatically extracts data from the current year and until last sunday.. The first part was fairly easy, but I'm struggling with dynamically adding last sunday..
Have read through 4-5 other threads on here, but have not been able to get anything from them, as the code just returns and error or the wrong results
I'm using MySQL Workbench 8.0.16 if that is of any help :)
"my.column" between DATE_FORMAT(CURDATE() ,'%Y-01-01') AND DATE_SUB(DATE(NOW()), INTERVAL DAYOFWEEK(NOW())-1 DAY)
Store Last Sunday in variable and use it
DECLARE #CurrentWeekday INT = DATEPART(WEEKDAY, GETDATE())
DECLARE #LastSunday DATETIME = DATEADD(day, -1 * (( #CurrentWeekday % 7) -1), GETDATE())
BETWEEN convert(datetime,'01-01-'+convert(varchar(10),datepart (year,getdate()))) and
#LastSunday

Converting MySQL code with date functions to MS SQL Server

Working with one of my applications, I am adding two databases support to my project, but I am very limited in my knowledge with MSSQL. My MySQL code is like below
SELECT count(ip_address) AS failed_login_attempt
FROM failed_login
WHERE ip_address = '$ip'
AND date BETWEEN DATE_SUB( NOW() , INTERVAL 1 DAY ) AND NOW()
and I am trying to do same kind of functionality with MSSQL:
SELECT count(ip_address) AS failed_login_attempt
FROM failed_login
WHERE ip_address = '$ip'
AND date between [tHIS pIECE OF cODE I DO NOT KNOW HOW TO FIx ]
SQL Server has several ways of returning the current date, for example getdate(). To subtract one day from the current datetime use the dateadd function. So this:
BETWEEN DATE_SUB( NOW() , INTERVAL 1 DAY ) AND NOW()
should be equivalent to
BETWEEN dateadd(day, -1, getdate()) and getdate()

MySQL select dependant on time of day

I want to select from a MySQL table and filter depending on the time of day:
if now > 10am select uploaded date where created date is today
if now < 10am select uploaded date where created date is yesterday
I think you must use Cron jobs for this.
Then, your sql will look like that:
WHERE tbl.TimeCreated = DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)
assuming this format of the field: DateTime(YYYY-MM-DD HH:MM:SS)
This should do what you need.
SELECT
`uploaded_date`
FROM
`table`
WHERE
`created_date` = CASE WHEN CURTIME() > 100000
THEN CURDATE()
ELSE CURDATE() - INTERVAL 1 DAY
END
You can see a similar example in this fiddle which should return a single row of "AM" in the morning, "Noon" at noon, and "PM" in the afternoon (and 2 empty result sets).

Calculate difference between dates

The title might be a bit misleading, but what I want is:
SELECT * FROM table ORDER BY pid ASC
And in one of the columns I have a DATE(). I want to compare the current date (not time) and return how many days are left till that date. Let's say the date is 2013-04-20 and today's date is 2013-04-16 I don't want to get any data if it's < current date. If it is I want it returned in days.
I've been looking around here and I've found no way to do it, and I can't for the love of me figure it out.
If you're looking for the difference between two date you can use the GETDATE function in MS SQL
SELECT DATEDIFF(DD, DateOne, DateTwo) FROM TABLE
This will return the difference in number of days between the two dates.
If you only want rows where the date field is less than or equal to today's date you can use:
SELECT DATEDIFF(DD, DateField, GETDATE())
FROM TableName
WHERE DateField <= GETDATE()
If you're using MySQL you can use DATEDIFF()
SELECT
DATEDIFF(NOW(), date_column) AS days_diff
FROM
tablename
Get the difference between two dates (ANSI SQL)
select the_date_column - current_date as days_left
from the_table
where the_date_column - current_date <= 4;
SQLFiddle: http://sqlfiddle.com/#!12/3148d/1

sql telling how many days have passed(datediff)

I'm working on a project where I want to display data after 3 days have passed.
What I'm having an issue with is getting the current date dynamically in php/sql. I'm aware of how to get the current date in php, but I dont know how to compare that value to the date that I have in the sql database.
You can do that directly in SQL
select * from your_table
where date_column <= curdate() - interval 3 day
You can use an interval select to limit the records to within 3 days if the column you're checking istimestamp, date, or datetime.
select * from tablename where timestamp_column >= NOW() - INTERVAL 3 DAY
You can use DATEDIFF function to check for days.
SELECT *
FROM tablename
WHERE DATEDIFF(CURRENT_DATE(), datecol) >= 3;