How do i retrieve data from YTD to last sunday? - mysql

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

Related

How to get 'Monday Date' in MySQL?

I have an Oracle SQL Query, which I am trying to re-write in MySQL.
PS : The dates are just arbitrary here, in the actual scenario I use this custom query inside Tableau where it accepts user defined Dates.The "Reference Start Date" is being compared to "Start Date".
Part of the Oracle SQL query:
CASE WHEN psr.dt_orgn IS NULL THEN NULL
ELSE
CASE WHEN CAST('2017-05-22' AS DATE) >= CAST('2017-06-22' AS DATE) THEN TRUNC(psr.dt_orgn,'IW')
ELSE TRUNC(psr.dt_orgn + ((CAST('2017-06-22' AS DATE)-CAST('2017-05-22' AS DATE))*(INTERVAL '1' DAY)),'IW')
END
END) week
The above uses an existing Calender table in a Database to convert dates into 'Week Start Dates' i.e Monday Date of that Week
MySQL version:
CASE WHEN psr.originated IS NULL THEN NULL
ELSE
CASE WHEN CAST('2017-05-22' AS DATE) >= CAST('2017-06-22' AS DATE) THEN DATE_ADD(psr.originated,
INTERVAL - WEEKDAY(psr.originated) DAY)
ELSE DATE_ADD(psr.originated + ((CAST('2017-06-22'AS DATE) - CAST('2017-05-22' AS DATE)) * (INTERVAL '1' DAY))
END
END) week
I am using a solution I found to get 'Week Start Date' in MySQL as follows:
DATE_ADD(mydate, INTERVAL(1-DAYOFWEEK(mydate)) DAY)
And I am getting error while doing this.
My question is how do I apply the same to the 2nd else condition with the '1 day Interval' in the MySQL query?
Or is there an easier solution to get week start date in MySQL?
I guess you're asking how to take any arbitrary DATE value and return the DATE value of the most recent Monday. In Oracle you can use TRUNC(datestamp, 'W') to do that.
Here's one way to do it in MySQL:
FROM_DAYS(TO_DAYS(datestamp) -MOD(TO_DAYS(datestamp) -2, 7))
If you wanted the Sunday, you would use
FROM_DAYS(TO_DAYS(datestamp) -MOD(TO_DAYS(datestamp) -1, 7))
I wrote it up here. http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/
You can get the day of a date using the function on MySQL called dayname(), for example dayname("2006-04-24") will return you Monday. In this case you only need to put the row that contains your date into the function. After that you can easily compare two dates.

how to Migrate Date_ADD method into Db2 for dynamic values

how to Migrate Date_ADD method into Db2 for dynamic values
where updatedDate BETWEEN ? AND DATE_ADD(?, Interval 1 day)
I need to run a dateadd function that will minus 1 DAY to current date
SQL has a function called dateadd, but it appears DB2 does not have this function. IS there anything equivalent to this function? If so, can someone post a syntax example?
THANKS
In DB2 you can directly add days ( eg. date + 10 days ) , months ( eg. date + 10 months) and years just like arithmetic addition.
The statement actually worked for me in DB2:
Birthdate + 52 Years as AGE 52_DATE
reference: http://www.dbforums.com/showthread.php?1637371-Help-Is-there-a-DATEADD-function-in-DB2

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 query to select a certain day in the past?

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

Compare datetime in stored procedure

I have a sql 2008 database and I am creating a stored procedure that shall check if a datetime is more than 3 hours old but I don't know how to do it.
Do you have some way to do it?
the datetime is a field in the table.
BR
Rather than applying DATEDIFF to the column value, which will negate an index, I suggest using a comparison of the column to an expression (which can use an index).
If you want this as a filter:
SELECT columns
FROM dbo.table
WHERE DateTimeColumn < DATEADD(HOUR, -3, CURRENT_TIMESTAMP);
(If you want only the rows that are newer than 3 hours old, change < to > or >=.)
If you want to return all rows with a column showing whether it is more than 3 hours old:
SELECT columns, [3HoursOld] = CASE
WHEN DateTimeColumn < DATEADD(HOUR, -3, CURRENT_TIMESTAMP)
THEN 'Yes, older than 3 hours.'
ELSE 'No, not older than 3 hours.'
END
FROM dbo.table;
Take at look at the DATEDIFF function.
DATEDIFF ( datepart , startdate , enddate )
You would then use with datepart set to hh and the enddate set to the current time. To get the current database time you could use GETDATE(). Compare the result with 3 since it will return the number of hours passed.
#date is the date you want to compare
declare #date datetime
set #date= '2012-02-15 14:20:42.797'
SELECT DateDiff(hh, DATEADD(hh,-3,#date), GETDATE()) --if it's > 3
you better create a Boolean function that does the trick that you can use where ever you like