Monthly Dates In MySQL - mysql

I'm having a bit of trouble currently. The system I'm trying to implement is a 'callback' system that will set callbacks against customers that will pop up on the system at the specified date.
One of the features is a reoccurring callback. The two options are monthly and yearly. I have the yearly ones working fine:
SELECT * FROM `callbacks` WHERE (
-- Yearly Callbacks
(DAYOFYEAR(`date_callback`) = DAYOFYEAR(NOW()) AND `occurs` = 'yearly') OR
-- Other Callbacks For Today
(DATE(`date_callback`) = DATE(NOW())))
But I'm having trouble visualising how to check if a callback that is monthly is due. It should only occur once every 30 days in the list (that uses the above query.) I was using DAYOFMONTH but I realised that if a callback was set for the 31st it wouldn't appear in the list for a few months at a time despite it being a monthly callback.
So if one is set for March 3rd 2013 and set for montly, it should appear in the results of the query if the date is:
March 3rd 2013
April 1st 2013
May 1st 2013
May 31st 2013
June 30th 2013
and so on. Has anyone dealt with this before?

I'd consider to use interval, something like
. . .
(DATE_ADD(NOW(), INTERVAL 1 MONTH))
. . .
See also this post.
Or - you can try (pseudocode)
now() - pre_saved_date % 30 (module of 30)
For % - use MOD function, like
select MOD(2, 3);
select 3 % 2;

Related

IsWeekEnd Expresion in SSIS that is locale-neutral

How can I write an SSIS expression that determines whether a date is a weekend day?
I have this expression, but it seems it only 'works on my machine'
DATEPART( "Weekday", #[User::CurrentDate] ) ==6 || DATEPART( "Weekday", #[User::CurrentDate] ) ==7
After I deploy SSIS package to server, is suddenly changes order of numbers and
Monday became 2.
When on my computer Monday is 1, exactly as I expected.
I checked SQL Server itself, but it seems is something inside SSIS decide if Sunday or Monday is first day.
I cannot find exact question related to this issue.
Well, if you must do this in an SSIS expression, here is one way to work around the problem: you would compare the DW of your variable to the DW of a known constant (like August 3, 2019, which is a Saturday in any locale that I know of).
In pseudocode, IF ( DW(DateVariable) = DW("20190803") OR DW(DateVariable) = (DW("20190803")+1)%7 ) THEN {DateVariable is a weekend}
Personally, I would look for a way to do this in TSQL. Import the raw data "as is" into a staging table, and then do the transformation while moving the data to the destination table with a stored procedure.
DATEPART("DW",GETDATE()) or DATEPART("WEEKDAY",GETDATE()) works perfectly fine on both SQL server and local.
How are you passing value to #[User::CurrentDate] variable(Please check if same date is set when packages is executed on SQL server and on local)
I changed DateTime on my local and tested your expression as part Of Execute SQL task and Derived Column Transformation which gives me 1 for Sunday, 2 for Monday and so on and the result matches with SQL.
SQL query to get day of week:
SELECT GETDATE() Today,DATENAME("DW", GETDATE()) DayofWeekToday,
DATEPART("DW",GETDATE()) DayNumberToday,GETDATE()-1 Yesterday,
DATENAME("DW", GETDATE()-1) DayofWeekYesterday,DATEPART("DW",GETDATE()-1) DayNumberYesterday
Adding more to my answer : There is a difference between DateFirst and DatePart. DateFirst sets first day of the week while Datepart gives you day of the week. use this sql function to check what's your first day of the week
Set Datefirst 7 -- To set first day of week (7 = Sunday)
SELECT ##DATEFIRST -- To check first day of week
**Update your system settings to match first day of the week with sql server and you should get same values when you evaluate expression.
Your expression looks good its only your first day of week on local and sql server are not matching.
Once I updated first day of week on SQL, I was able to replicate the issue you are facing.

Graph - just show last 12 months

I have a line graph in my report.
My report has no parameters on it.
What I ideally want it to do is if I run the report today (May) it will only show me the last 12 months so May to May.
At the moment it is including April from last year as well, so would want that excluded. If I run it in two months time in July I would want it to only show July to July.
Hi,
The key here is the DatePaid field. This is the date that drives the graph.
The graph gets the Financial Year and Month from a date table.
The plots on the graph use an expression -
[code="other"]=SUM(IIF(Fields!InvoicePaid_.Value = "Yes" And Fields!PaidTarget.Value = "InTarget",1, 0))/Count(IIF(Fields!InvoicePaid_.Value = "Yes", 1, Nothing))[/code]
This allows a % to be calculated on the graph series you see above, to just show invoices that have been paid and are in target.
The report uses TSQL and I'm wondering if there is a flag I can place against any invoice that was paid 12 months ago. And then via a filter remove them from the graph?
So today date is 02/06/2015. I would want to mark all invoices with a flag upto 01/06/14 so they all appear on the graph - but May and April 2014 would be excluded.
Then when I run the report next month say - 7th of July 2015 - June 2014 will no longer appear on the graph but the 01/07/2014 and onwards would?
Hope that makes sense.

How to get MySQL to return the correct year number for "week 53 of 2012" using "mode 7"

How would I get MySQL to tell me that '2013-01-01' is week number 53 of 2012?
I'm using mode 7 for the definition of a week.
I can get the week number easily enough by using WEEK('2013-01-01', 7)
The problem is that, of course, YEAR('2013-01-01') returns 2013.
The YEARWEEK function lets you pass the mode to it as well. The only downside is that it returns the year and week-in-year combined together:
SELECT YEARWEEK('2013-01-01', 7);
>> 201253
You can get around that with SUBSTRING, though, if you really need just the year separately:
SELECT SUBSTRING(YEARWEEK('2013-01-01', 7), 1, 4);
>> 2012

Past 5 week calculation in WEBI (BO 4.0)?

I am facing issue in calculating past 5 week data based on current date (excluding currrent week)
For e.g:
Suppose we are in 40th week of this year, I need to get the sum of all the transactions for the previous 5 weeks (39, 38, 37, 36 & 35).
Currently calculating based on the calendar day but as Calendar day is giving the granular level of data, inorder to increase the performance I need to use the calendar week (sample data like (2012/40).
Is there a way to do this?
I'd construct a flag field (either in the back-end or in the universe) using datediff (in SQL Server terms).
Failing that, you could construct a variable in Web Intelligence using the Week function.
Pseudo-code but something like:
=IF(Transaction_Date < Week(CurrentDate()) AND Transaction_Date >= (Week(CurrentDate())-5); "TRUE"; "FALSE")

Get the week number from a given date

Examples:
'DD/MM/YYYY
"1/1/2009" should give `1`
"31/1/2009" should give `5`
"1/2/2009" should also give `5`
Format("1/2/2009", "ww") returns 6.
So, how can I get the correct result?
It's doing two things here which don't match your expectations, I think:
Assuming you want the week with Jan 1 in as week 1, and using Sunday as first day of the week
So it has week 1 running from Sunday 28th December 2008 to Saturday 3rd Jan 2009.
Week 6 would begin on Sunday 1st Feb by this method.
The ISO standard is for week 1 to be the one containing 4 days of January, or the first Thursday of the year (different ways of expressing the same thing).
You can specify this method of calculation and the first day of the week:
Format(SomeDate,"ww",vbMonday,vbFirstFourDays)
see here for syntax:
https://support.office.com/en-US/article/Format-Function-6F29D87B-8761-408D-81D3-63B9CD842530
Regardless of the day of the week your week starts on, you need to pass unambiguous date values. "31/1/2009" can only be one date (Jan 31st), but "1/2/2009" could be Jan. 2 (US style) or Feb. 1st (everybody else who has more sense that we USAns).
In this case, I'd use DateSerial() to make sure the date is not misinterpreted:
Format(DateSerial(2009,2,1), "ww", vbMonday)
While this is not causing your problem, because Access helpfully utilizes your system's localized date settings, I think it's something you should do anyway. You certainly are forced to do so in SQL in Access, so I don't think it's a bad habit in code and expressions.
This might work: Format(YourDate, "ww",vbMonday)
"Correct result" depends on the locale. Maybe VBA will let you pick a calendar-system, otherwise you're pretty much out of luck.
Note that First-Day-On-xxDay isn't your only problem. There is also variation on what a complete week is so Week 1 in one system could be Week 53 of the previous year in another system.
So test thoroughly and don't be seduced to "correct by 1".
There is a whole standard for week numbers: ISO-8601
http://en.wikipedia.org/wiki/ISO_8601#Week_dates
I had the same problem.
It showed week 53 and week 1, yet days in week 53 and week 1 are all in week 1
I first tried changing the date format in the Access Query to this:
OrderWeek: Format([OrderDate],"yyyy-ww",1,3) <-- But it did not do the trick.
You get dates like 2014-52 for week 52 and 2015-52 where it was week 1 before.
Also the sorting was not how I liked. It sorted the data as 2014-1, 2014-11, 2014-2 etc. I want it to show as 2014-01, 2014-02 .. 2014-11 etc.
So here is the new code to display both the year and the week correctly in an Access Query:
ActualWeek: IIf(DatePart("ww",[SomeDate])=53,DatePart("yyyy",[SomeDate])+1,DatePart("yyyy",[SomeDate])) & "-" & IIf(DatePart("ww",[SomeDate])=53,"01",IIf(DatePart("ww",[SomeDate])<10,"0" & DatePart("ww",[SomeDate]),DatePart("ww",[SomeDate])))
This now shows any days from week 53 as being part of week 1
If sunday is the first day of the week (as it is in some locales) then 6 is the correct weeknumber for "1/2/2009" (february 1. 2009)
In terms of the sorting, I had the same issue and used this code to resolve it:
IIf(Format([SomeDate],"ww")<10,Format([SomeDate],"yyyy-") & "0" & Format([SomeDate],"ww"),Format([SomeDate],"yyyy-ww"))
If the week number is less than 10, add a zero, else leave it as is.
Now the sorting is fine. Hope this helps somebody.