int_months_between for weeks in impala? - cloudera

What would be the best solution for int_months_betweenin weeks for Impala?
Would I have to work with Invervals or what is the best recommendation.

The easiest way would be to use datediff function, which returns difference in days, and then to divide the result with 7. It will not be precise in terms of business weeks, and there might be some ambiguous results at the beginning or end of the year, but in general it should do the trick. This is function signature for Impala 5.8.x:
datediff(timestamp enddate, timestamp startdate)

Related

IBM Cognos Parameter Syntax for last month

I have a report in Cognos, in this report I have a date filter that will be passed by the user and I want the result set to include dates from the last 30 days. Essentially (?date? - <30days>) I am unfamiliar with the syntax to accomplish this because date is not a primitive type.
[Time stamp] <= ?date?
and
[Time stamp] >= (?date? - <30days>)
I would appreciate any advice you guys may have.
Use [Time stamp] >= _add_days( current_date(), -30)
In case you in fact need a month, not just 30 days, you might want to use _add_months(?date?, -1)

Dates in SQLite3, with a twist (inaccurate dates)

I am working on genealogical software that stores its data in SQLite3 format. Everything works fine, except for one minor detail. Not in all cases is the accuracy of the birth or death dates (etc) available to the exact day. So I have the following accuracies:
exact (YYYY-MM-DD)
month (YYYY-MM)
year (YYYY)
year (YYYY+/-5)
year (YYYY+/-10)
year (YYYY+/-50)
decade
century
Now, assuming I store everything in a single column, I end up with a problem. Since SQLite3 has the Julian Day function I was thinking to encode the accuracy in the fractional part of the REAL Julian Day (I don't need the hours anyway). That is fine, but it complicates the way SELECTs work, in fact it means that stuff I could otherwise offload to SQLite3 has to be implemented in application code.
What would be a reasonable method to store the inaccurate dates and be able to query them quickly?
Note: if it matters to anyone answering, the language used is Python, but I am asking in general.
When doing queries on those date values, the most common operation probably is to check whether a date might match another date.
For this, you always need the start and the end of the interval, so it would make sense to store these two values in the DB.
(Call them Start/End or Min/Max or Earliest/Latest or whatever makes sense.)
For example, to find people who might have been born one century ago:
... WHERE '1913-04-16' BETWEEN BirthDateMin AND BirthDateMax
Inequality comparisons can be done with one of the interval boundaries.
For example, to find people who might have been born more than one century ago:
... WHERE BirthDateMin < '1913-04-16'
Just because you're storing date information, doesn't mean that the built-in date type is the right one for you. Your data requirements (date inaccuracy) means that it's probably more accurate and better long-term to do some custom date-handling work, and avoid using the built-in date data types.
Use two columns. One column is the approximate date, as accurate as possible, in SQLite format. The second column is the accuracy of the date in days. If the date is absolutely accurate, the second column is zero. If only the month is known, the date would be mid month and the second column 15 days. Etc. Date comparisons can be done by comparing against the date +/- the accuracy column.

Date arithmetic in Erlang

I have a date in this format {Y,M,D}. Is there any good supporting libraries or, tricks I can use to simply, say subtract three months from this date without running into problem with invalid dates, leap years, etc.
My latest similar use is in MySql where you can type:
Select '2011-05-31' - Interval 3 Month;
which yields '2011-02-28'. I am not interested in how to write this library myself, that is what I would like to avoid.
1> calendar:gregorian_days_to_date(calendar:date_to_gregorian_days({2011, 7, 14}) - 90).
{2011,4,15}
http://www.erlang.org/doc/man/calendar.html
Use edate - specifically the shift function.
> edate:shift({2011,5,31}, -3, months).
{2011,2,28}
Under the hood it uses the calendar module, so it correctly deals with all of the corner cases.
I created a GitHub gist with some useful utilities for this sort of thing. Feel free to steal from it if you want.
https://gist.github.com/104903

How do I get the last day on the month using SQL Reporting Services

In SQL Server Reporting Services, how would I calculate the last day of the current month?
Here is the answer I came up with
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(1).AddDays(-1)
As it took me a while to Figure this out, and JC's answer was the simplest to modify and had a good logical structure. I expanded it Slightly for others searching for answers on this topic.
I have found normally you don't just want the Last Day of a month / year you also want the first day of the month / year. So here they are the full lines to calculate just that. wtih the SQl version there also.
First Day of Current month
SSRS=Today.AddDays(1-Today.Day)
SQL=SELECT DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,getdate()),0))
Last day of Current Month
SSRS=Today.AddDays(1-Today.Day).AddMonths(1).AddDays(-1)
SQL=SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
First Day of Current year
SSRS=Today.AddMonths(1-Today.month).AddDays(1-Today.day)
SQL=SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
Last Day of Current Year
SSRS=Today.AddDays(1-Today.Day).AddMonths(13-today.month).AddDays(-1)
SQL=SELECT DATEADD(dd,-1,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0)))
I hope this helps somone.
I know you've found your own answer, but I'd suggest this alternative:
=Today.AddDays(1-Today.Day).AddMonths(1).AddDays(-1)
It's a little easier to read, in my opinion, and might have slightly better performance (though most likely unnoticeable)
And, of course, if you wanted to pad out that date to 23:59:59, as is often necessary, just modify slightly:
=Today.AddDays(1-Today.Day).AddMonths(1).AddSeconds(-1)
From the blog of a Microsoft SQL Team member:
-- returns the last day of the current month.
select dbo.Date(year(getdate()), month(getdate())+1,0)
http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx
Hope this helps!
--Dubs
You may try this expression, Just replace now with your date field.
=DateSerial(Year(Now), Month(Now), 1)
Hope this helps.
Regards
There is an even easier way as in the marked answer:
=DateSerial(Year(Now()), Month(Now())+1, 0)
Since DateSerial() accepts integers as parameters one does not have to use AddMonths() and AddDays(). As in the example an instant calculation inside DateSerial() is possible.
Furthermore day 1 is the first day of the month while the first day minus one day is the last day of the month before (1-1=0). So the example will return the date of the last day of the current month.
you can use an assembly for doing this work by adding it as a reference.

get day of the week from yyyy-mm-dd

I would like to convert a yyyy-mm-dd to something like this:
"Saturday, 2 October 2009"
I would like also to have the option to modify the language both day of the week and month (make it customizable)
thanks in advance ;)
Since you never told us the language, here's a discussion on how to do it in COBOL.
Assuming .NET (from the datetime tag):
DateTime.Parse("2009-10-02").ToString("D", CultureInfo.CreateSpecificCulture("en"));
In C, you would use a combination of the localtime() and strftime() functions. They should handle internationalization more or less automatically, if your application is set up for it.
Convert the date into an integer, representing the days since a specific date, then add a number and apply the modulus operator with the operand 7. That will give you a number 0-6 that represents the day of week.
However, most languages have this functionality built-in.

Resources