ms-access built in function Month(number) - sql

I Have been playing with variations of the Month... function in access query builder. I am having trouble building a date value from an expression. I am looking to create my own date that will be behind the scenes to perform some filtering and other tasks. My problem is that I cant seem to get the Month(number) function to do what I think it should be doing. Here is a summary of what I am looking for.
5/31/2012
Through something like this
DateSerial(Year(Date()),Month(5),Day(31))
Also
DateSerial(Year(Date()),Month("5"),Day("31"))
When I try these as an experssion the return is
1/30/2012
Im sure I am misunderstanding the structure. Please educate me.

DateSerial requires three integers, year, month, day:
DateSerial(1992,5,2)
02/05/1992 ''Euro locale
Year(Date()) returns an integer, so you can substitute:
DateSerial(Year(Date()),5,31)
Interestingly, the zeroth day is the last day of the previous month:
DateSerial(2012,12,0)=30/11/2012
-- http://office.microsoft.com/en-ie/access-help/HV080206953.aspx
As an aside, do not forget that all dates are numbers.
Month(5) will equal 1, but Month(41263)=12 !
Also
?month(100)
4
?Year(100)
1900

Related

Enter date into function without quotes, return date

I'm trying to write a function of this form:
Function cont(requestdate As Date)
cont = requestdate
End Function
Unfortunately, when I enter =cont(12/12/2012) into a cell, I do not get my date back. I get a very small number, which I think equals 12 divided by 12 divided by 2012. How can I get this to give me back the date? I do not want the user to have to enter =cont("12/12/2012").
I've attempted to google for an answer, unfortunately, I have not found anything helpful. Please let me know if my vocabulary is correct.
Let's say my user pulled a report with 3 columns, a, b and c. a has beginning of quarter balances, b has end of quarter balances and c has a first and last name. I want my user to put in column d: =cont(a1,b1,c1,12/12/2012) and make it create something like:
BOQ IS 1200, EOQ IS 1300, NAME IS EDDARD STARK, DATE IS 12/12/2012
So we could load this into a database. I apologize for the lack of info the first time around. To be honest, this function wouldn't save me a ton of time. I'm just trying to learn VBA, and thought this would be a good exercise... Then I got stuck.
Hard to tell what you are really trying to accomplish.
Function cont(requestdate As String) As String
cont = Format(Replace(requestdate, ".", "/"), "'mm_dd_YYYY")
End Function
This code will take a string that Excel does not recognize as a number e.g. 12.12.12 and formats it (about the only useful thing I can think of for this UDF) and return it as a string (that is not a number or date) to a cell that is formatted as text.
You can get as fancy as you like in processing the string entered and formatting the string returned - just that BOTH can never be a number or a date (or anything else Excel recognizes.)
There is no way to do exactly what you're trying to do. I will try to explain why.
You might think that because your function requires a Date argument, that this somehow forces or should force that 12/12/2012 to be treated as a Date. And it is treated as a Date — but only after it's evaluated (only if the evaluated expression cannot be interpreted as a Date, then you will get an error).
Why does Excel evaluate this before the function receives it?
Without requiring string qualifiers, how could the application possibly know what type of data you intended, or whether you intended for that to be evaluated? It could not possibly know, so there would be chaos.
Perhaps this is best illustrated by example. Using your function:
=Cont(1/1/0000) should raise an error.
Or consider a very simple formula:
=1/2
Should this formula return .5 (double) or January 2 (date) or should it return "1/2" (string literal)? Ultimately, it has to do one of these, and do that one thing consistently, and the one thing that Excel will do in this case is to evaluate the expression.
TL;DR
Your problem is that unqualified expression will be evaluated before being passed, and this is done to avoid confusion or ambiguity (per examples).
Here is my method for allowing quick date entry into a User Defined Function without wrapping the date in quotes:
Function cont(requestdate As Double) As Date
cont = CDate((Mid(Application.Caller.Formula, 7, 10)))
End Function
The UDF call lines up with the OP's initial request:
=cont(12/12/2012)
I believe that this method would adapt just fine for the OP's more complex ask, but suggest moving the date to the beginning of the call:
=cont(12/12/2012,a1,b1,c1)
I fully expect that this method can be optimized for both speed and flexibility. Working on a project now that might require me to further dig into the speed piece, but it suits my needs in the meantime. Will update if anything useful turns up.
Brief Explanation
Application.Caller returns a Range containing the cell that called the UDF. (See Caveat #2)
Mid returns part of a string (the formula from the range that called the UDF in this case) starting at the specified character count (7) of the specified length (10).
CDate may not actually be necessary, but forces the value into date format if possible.
Caveats
This does require use of the full dd/mm/yyyy (1/1/2012 would fail) but pleasantly still works with my preferred yyyy/mm/dd format as well as covering some other delimiters. dd-mm-yyyy or dd+mm+yyyy would work, but dd.mm.yyyy will not because excel does not recognize it as a valid number.
Additional work would be necessary for this to function as part of a multi-cell array formula because Application.Caller returns a range containing all of the associated cells in that case.
There is no error handling, and =cont(123) or =cont(derp) (basically anything not dd/mm/yyy) will naturally fail.
Disclaimers
A quick note to the folks who are questioning the wisdom of a UDF here: I've got a big grid of items and their associated tasks. With no arguments, my UDF calculates due dates based on a number of item and task parameters. When the optional date is included, the UDF returns a delta between the actual date and what was calculated. I use this delta to monitor and calibrate my calculated due dates.
All of this can absolutely be performed without the UDF, but bulk entry would be considerably more challenging to say the least.
Removing the need for quotes sets my data entry up such that loading =cont( into the clipboard allows my left hand to F2/ctrl-v/tab while my right hand furiously enters dates on the numpad without need to frequently (and awkwardly) shift left-hand position for a shift+'.

Return value of ssas dimension property?

I have two dimension properties "Project Start" and "Project End" that belong to my [Project] dimension. They stand for the start and end date of my projects. Now I want to use these values with MDX, or to be more precisely, in an IIF statement in my query. I want to now if one or both properties are after or before some parameters (projectstart ([Time].[Year - Month - Day].[Month].&[2012-01-01T00:00:00]) and projectend).
So I started to try things like this:
Format([Project].[ParentProject].CURRENTMEMBER.PROPERTIES("Project End"), "yyyy-mm-dd") > StrToMember("[Time].[Year - Month - Day].[Month].&[2012-01-01T00:00:00]")
[Time].[Year - Month - Day].[Month].&[2012-01-01T00:00:00].MEMBER_KEY > [Project].[ParentProject].CurrentMember.Properties("Project End")
CDate([Time].[Year - Month - Day].[Month].&[2012-01-01T00:00:00].MEMBER_KEY) > CDate([Project].[ParentProject].CurrentMember.Properties("Project End"))
But either I get an error or the wrong case of the IIF-statement is executed. After chechkig up MSDN what I get from "[Project].[ParentProject].CurrentMember.Properties("Project End")" I'm more than confused now. On the one hand it says that I will get a string, but on the other hand (if there is a TYPED in the property definition) I should get back the data type of the value (in this case DateTime). I wonder what is right now and/or if I understood the MSDN wrong.
Maybe somebody can help me to clear that up and give me a hint how I can work with these properties.
Okay, after spending some more time on finding the answer I have changed my MDX query quite a lot. This resulted in the fact, that I ended up with a query including the Format function and "yyyy-mm-dd" as aiming format. But the conversion didn't work and said, that it can't convert my property value into a date. The returned value of the property was at least for a human recognisable as a date (I wrote a simple MDX query to have a look at what I get).
So obviously the data type of a dimension property seems really to be a string.

Excel Concatenate month

I am trying to take the following value and making it into a date format of dd-MMM-yy
For example: 110412 turns into 04-NOV-12
My formula as of right now is:
(CONCATENATE(MID(E14,3,2),"-",(TEXT(LEFT(E14,2),"MMM")),"-",RIGHT(E14,2)))
It is giving me 04-Jan-12.
Please note that I would like the month to be in all caps.
Try using the "UPPER(foo)" function as a wrapper to make the result all caps.
UPPER((CONCATENATE(MID(E14,3,2),"-",(TEXT(LEFT(E14,2),"MMM")),"-",RIGHT(E14,2))))
or better still:
=MID(E14,3,2)&"-"&UPPER(TEXT(DATE(MID(E14,5,2),MID(E14,1,2),MID(E14,3,2)),"MMM"))&"-"&RIGHT(E14,2)

find the difference between two datetimes in seconds

I have had a quick look around and I can't find an answer to this in vb.net or something that I can convert into vb.net.
I have two DateTimes in vb.net's 'Date' class. I would like to find the difference between these in seconds. I can do a-b, but the answer will still be a 'date'. I can use .seconds .minutes etc. and multiply but I will hit problems when I come to months.
Is there a simple way to do this, or do I need to write some elaborate-ish code?
Many Thanks
Subtract the DateTime values from each other - the returned type will be a TimeSpan.
Get the TotalSeconds value from it.
(date1 - date2).TotalSeconds
There actually is also a function made for this DateDiff(DateInterval.Second, d1, d2)
How about something like
Dim secs As Double = DateTime.Today.Subtract(DateTime.Today.AddDays(-1)).TotalSeconds
Look at DateTime.Subtract Method (DateTime) and TimeSpan.TotalSeconds Property

Reporting Services - handling an empty date?

Hey, I have a report parameter which looks like this: 01.01.2009 00:00:00
Its a date (as string), as you might have guessed :). The problem is, this param can be an empty string as well. So I tried those expressions:
=IIf(IsDate(Parameters!DateTo.Value), CDate(Parameters!DateTo.Value), "")
=IIf(Len(Parameters!DateTo.Value) > 0, CDate(Parameters!DateTo.Value), "")
Both dont work and the value for the textfield where I print the expressions result is always #Error. As soon as I remove the CDate stuff, it works, but I have to use it. IS there another way to achieve that? What I want is to display nothing if its not a date or the date (format dd.mm.yyyy) if its a date.
Ideas?
Thanks :)
All arguments to the IIf are evaluated, which results in your error, since the CDate will fail for an empty string.
You can get around this by just writting a function along these lines, using a standard if statement:
Function FormatDate(ByVal s As String) As String
If (s <> "") Then
Return CDate(s).ToString()
Else
Return ""
End If
End Function
Then call it with: =Code.FormatDate(Parameters!DateTo.Value)
First, fix your database to properly store dates rather than doing these workarounds. You probably have bad data in there as well (Feb 30 2010 for example or my favorite, ASAP). Truly there is no excuse for not fixing this at the database level where it needs to be fixed except if this is vendor provided software that you can't change (I would yell at them though, well notify them really, and ask them to fix their data model or go to a new product designed by someone who knows what they are doing. A vendor who can't use dates properly is likely to have software that is very poor all around).
In the query that you use to select the infomation, have you considered just converting all non-dates to null?