FROM_DAYS() not giving expected output[MySQL] - mysql

The official documentation says that FROM_DAYS returns a valid date given number of days N.
Then why is this happening ?
Should it not display the valid dates for days less than a year, like
from_days(5) = 0000-01-05 ?
Edit : I was solving a question which went as "Find the period an employee has worked given their date of joining and report the duration in the format years,months,days"
And I was using the following query :
select Emp_Name,
date_format(from_days(datediff(curdate(),Date_of_Join)),"%y") as year,
date_format(from_days(datediff(curdate(),Date_of_Join)),"%m") as month,
date_format(from_days(datediff(curdate(),Date_of_Join)),"%d") as day
from EMPLOYEE;
It was in these results that I discovered this discrepancy.

As the MySql manual reports:
The function is not designed for use with dates before the advent of
the Gregorian calendar in October 1582. Results will not be reliable
since it doesn't account for the lost days when the calendar changed
from the Julian calendar.
So, if you set the argument of this function with a small number (around N < 1582*365), it is not reliable.

Related

Why am I getting a "type mismatch" error in my Accessdb query?

I am running a query in Access Database that should aggregate information based on year, month, and type. I set it up so that for the month, it would read the MonthName(InfoDate,1) to return the abbreviated month name. When I have all the query items in the query except the summation, it will run. As soon as I through the summation in, I get a "Type Mismatch." upon troubleshooting, I found that the issue was with the MonthName(InfoDate,1). If I remove the MonthName(InfoDate,1) and only include the Month(InfoDate) the query runs as expected. I want to keep the MonthName(InfoDate,1) in the query so that it is easily discernible as to what month we are discussing because the fiscal year does not start on Jan 1.
Thoughts on how to fix the "Type Mismatch." and run the query as expected?
It's the name of a month, not of a date, so (True or False as second parameter):
MonthName(Month(InfoDate), True)
To handle fiscal years, study module DateBank.bas at VBA.Date.

Why do two MySQL dates in 1582 appear to be the same but comparison result is false?

I know that Gregorian calendar started on Oct 15th 1582, and during the transition from Julian calendar, 10 days had been dropped.
When I'm doing this query:
SELECT STR_TO_DATE('1582-10-05', '%Y-%m-%d')
I'm getting this result:
1582-10-15 (the 10 days difference).
But when I'm trying to match between such dates I'm getting the original date (Oct 5th and not 15th).
For example:
SELECT STR_TO_DATE('1582-10-05', '%Y-%m-%d') = STR_TO_DATE('1582-10-15', '%Y-%m-%d')
I'm getting a false response, although you would have expected to get a true since Oct 5th actually count as Oct 15th, as we saw in the first example.
Anyone can explain what's going on here?
On documentation it is stated that, TO_DAYS and FROM_DAYS functions must be called cautiously because of the transformation you noticed. Additionally, when I inspect the source codes of MySQL, I realized that STR_TO_DATE uses similar methodology with these functions. As I understand, cutover dates are completely unsafe to store or apply operations. Documentation says; "Dates during a cutover are nonexistent.", too.
Also for the inconsistency between different servers I may have an explanation. I have 2 different machines which have MySQL installed in Istanbul, Turkey and Frankfurt, Germany. They have same setup excluding localisation settings. First one shows 1, the other one shows 11 for the date substraction query. It means (in my humble opinion) there is unexplained sections about calendar cutover & localisation on official documentation.
Please see the following results:
SELECT STR_TO_DATE('1582-10-05', '%Y-%m-%d');
# Result #1: 1582-10-15
SELECT DATE_FORMAT(STR_TO_DATE('1582-10-05', '%Y-%m-%d'), '%Y-%m-%d');
# Result #2: 1582-10-05
SELECT DATE_FORMAT(STR_TO_DATE('1582-10-15', '%Y-%m-%d'), '%Y-%m-%d');
# Result #3: 1582-10-15
SQL fiddle demo.
These indicate the problem lies in the way the 1582-10-05 date is displayed rather than how it is stored. Result #2 shows that if the DATE_FORMAT function is used instead to explicitly convert the date into the same string format then the input date is displayed. This also explains why the comparison query in the question returns false: Behind the scenes, the two stored dates are different.
As you've discovered, this quirk occurs for all dates between 1582-10-05 and 1582-10-14 inclusive, i.e. the range of dates that don't really exist: The implicit conversion to text for all of these gives a date 10 days after. So if for some reason there is a need to display dates in this range (perhaps questionable), a simple workaround is to always use the DATE_FORMAT function.

Netezza What is the best way to get the first day of the month for a date?

Select to_date(to_char(date_part('year',current_date),'0000') || trim(to_char(date_part('month',current_date),'00')) || '01','YYYYMMDD')
So far, this is the best I can come up with.
I am also unable to find a comprehensive language reference for Netezza SQL that has all functions in it, so please include a source in your answer.
Use date_trunc('month', current_ date), which is documented here.
Start with the date, for what I needed today, was using current date/month. Then find the last day of the current month. That's the outer shell for the first day as a systematic output:
start with the current date
use add_months (-1) to subtract a month;
find the last day of the previous month;
add 1 day.
I then decided to test out on February (leap year and normal), finding the first from the first, and then adding in finding the first of the next month. Also checked looking at a month ago from a month ending 31st and 30th. I think this should be flexible.
select current_date,
last_day(add_months(current_date,-1))+1 as firstdt_currmos,
last_day(current_date) as lastdt_currmos
;
select last_day(add_months('02-29-2016',-1))+1 as firstdt_febleap,
last_day(add_months('02-28-2019',-1))+1 as firstdt_febnorm,
last_day(add_months('07-01-2019',-1))+1 as firstdt_first,
last_day(add_months(current_date,-1))+1 as firstdt_currmos,
last_day(current_date)+1 as firstdt_nextmos,
last_day(add_months('07-31-2019',-1))+1 as firstdt_mos31st,
date(add_months('07-31-2019',-1)) as mosago_31st,
date(add_months('06-30-2019',-1)) as mosago_30th
;

SSRS - weekday name

any advice appreciated
I have as a column heading the expression =WeekdayName(weekday(fields!date.value))
This returns the day of the week, however, it is returning a day of the week one day in advance, eg when I put Monday's dates in the parameter it shows as 'Tuesday' in the report.
My question is can the above expression be tweaked to show the WeekdayName one day before, eg =WeekdayName(weekday(fields!date.value -1)) ? I tried this but got an error.
Thanks.
So you want to subtract one day from the your incoming date then you can use the
= DateAdd("d", -1, yourdateField)
This way you can subtract the any number of days from your date.
But did you try to see why it is giving the day of previous date. Do check the system date time or else check with the
=WeekdayName(weekday(Today()))
and see if it gives you the correct day of week for current date.
Weekday and weekdayname functions have both another optional argument for defining the starting day of the week.
Problem is the 2 functions don' t default this argument to the same value so depending on your server settings you should explicitly set the second argument of these functions.
No need to invoke a date function. As the weekday() function returns and integer you can offset the result and use the Mod operator to keep it in bounds:
=WeekdayName((weekday(fields!date.value)+5) Mod 7)+1)
The parenthesis are important to ensure the addition comes first.
Just for reference:
OP asked again because of the weekday offset and this is the correct provided solution.
=WeekdayName(weekday(Today())) gives me tomorrow
=WeekdayName(Weekday(Today(),FirstDayOfWeek.System))

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")