Converting a UserResponse with ToDate returning only January in Business Objects - sap

I am trying to convert a User Response from a report's prompt into a date. The ToDate() and FormatDate() functions work correctly, but only return the month of January.
I created a detail object called [xUser Response] whose formula is:
=UserResponse([Hours]; "As of Period YYYYMM"),
where [Hours] is the data source and "As of Period YYYYMM" is the prompt string.
Then, I used this detail object in another formula:
="Data as of "+ FormatDate(LastDayOfMonth(ToDate([xUser Response]; "yyyymm")); "Mmmm dd, yyyy")
The formatting applied through the other functions is all correct, but the date only gives me January, no matter what month I submit in the query prompt. If you know how to get around this, please let me know.
Thanks.

You should use yyyyMM as a format mask to convert your string to a date. mm is used to denote minutes.
Thus, your formula would be ="Data as of "+ FormatDate(LastDayOfMonth(ToDate([xUser Response]; "yyyyMM")); "Mmmm dd, yyyy")

Related

Does putting a date format on a date field make a difference in vba?

I've been looking and how people write code and was wondering if placing a format on a date field makes a difference. For example I saw this Format(EndDate, "mm/dd/yyyy") in a query but EndDate is defined as a Date field in the Table.
What are the benefits and problems that may arise for doing this?
strEDate = "#" & Format(EndDate, "mm/dd/yyyy") & "#"
strHSurg = "SELECT TOP 1 [Health Surcharge]FROM tblTax WHERE [Effective Date] <= strEDate " ORDER BY [Effective Date] DESC;"
If EndDate is a Date field couldn't you have the query like this
strHSurg = "SELECT TOP 1 [Health Surcharge]FROM tblTax WHERE [Effective Date] <= " & EDate & " ORDER BY [Effective Date] DESC;"
Does it matter? Or is it done so if the database is moved to a computer with the dd/mm/yyyy format the formulas will not be affected?
Format takes a Date variable/field and converts it to String. The difference between the two is far more than visuals. You cannot perform date type functions (like DateAdd) on a String or String functions (like Replace) on a Date.
I generally use the Format function when I am adding the date to a string. That can be a string the user will see (for example in a report header you can have different outputs like "Total Sales for 1/1/2020" or "Total Sales for January 1, 2020") or when generating an SQL query string to make sure the expected date format is what Access expects and avoid localization issues.
In every case the data is kept as a Date data type. I never store date data as String.
I like to format my dates using VBA because you never know what the formatting of a field may be. If there is a specific date format on the field you are writing to then the field format overrides the format assigned by VBA. Just like typing in the field. You can enter the data in any format, the field formatting will coalesce the data in to it's stated format.
To be safe, use the same format in VBA that you would expect the field to have.

Date format change SSRS

I have a view with a date column with this format : YYYY-MM-DD and I am using that column in a SSRS report but the final user wants to have something like this:
December 31, 2018
I am using this expression:
=FormatDateTime(Parameters!prmReportDate.Value,DateFormat.LongDate)
But I am getting this:
Monday December 31, 2018
I don't need the day. Is there a way to remove it?
You could simply format the textbox as a date and specify the correct format. You'll just need to set the expression in the textbox as:
=Parameters!prmReportDate.Value
And you should be able to set the Number format property as:
And this should give you the output you expect. If it doesn't work as expected(which actually seems to be the case as I test it), should be able to apply the following expression:
=Format(Parameters!prmReportDate.Value, "MMMM dd, yyyy")
Just remove one of the ds from the expression to remove a leading zero on single digit dates.
Try something like this:
=MonthName(Month(Parameters!prmReportDate.Value),false)
& " " &
day(Parameters!prmReportDate.Value)
& "," &
year(Parameters!prmReportDate.Value)

Access Date Time convert to string. Time is stored as Military, Don't want to convert

I am writing a SQL query for MS Access.
The two fields are DateTime. The start/end time are 22:30/6:30
When I run the following query
SELECT cDate(Format(table1.BeginTime,"hh:mm")),
I get 10:30 PM instead of 22:30. I do not want to convert to 12hrs format. How do I keep the Miliary time format?
So I tried a few things in Access 2013 and it seems that #Bjones has a point.
In SQL Server Format is a clr .net function and format(date, 'hh:mm') will be 12 hour and Format(date, 'HH:mm') will be 24 hour.
But as #Bjones pointed out there may be some question of how your original column is held as text or date or time..... And what I have concluded after trying is that your order of functions is wrong.
cDate(Format(table1.BeginTime,"HH:mm"))
cDate transforms the content to a date but it doesn't format it.
So
FORMAT(cDate(table1.BeginTime), "hh:mm")
or
FORMAT(cDate(table1.BeginTime), "HH:mm")
or
FORMAT(cDate(table1.BeginTime), "Short Time")
should give you what you want if the time is stored as text. If stored as date time you can just drop the cDate all together and stay with
FORMAT(table1.BeginTime, "Short Time")
as #Bjones answered.
You don't tell what the result of the query is intended for.
If you need the date value and to view this in 24 hour format, use:
Select table1.BeginTime
and apply a Format property for the column of h:nn or hh:nn
If you need the date formatted as a string in 24 hour format, use:
Select Format(table1.BeginTime, "hh:nn")
and no Format is needed.
In any case, even though the format string h:mm will work, do use h:nn or hh:nn, as m is for the month.
I think what you need to get rid of is the cDate function as your fields are already in DateTime format.
SELECT Format(table1.BeginTime,"hh:nn")

VBA Issues converting string to date using format() or CDate()

If this has been asked before, please point me in the right direction. I can't seem to find anything useful with my google-ing skills.
I have the following code, which reads in a string like this; Outage StartDate: 05/10/11 23:59 EST
And pulls out the date information, i.e. 05/10/11 23:59
sStartTime = Mid(objItem.Body, 18, 15) ' Extract the start time
Debug.Print "sStartTime after reading: " & sStartTime
sStartTime = Format(sStartTime, "dd/mm/yy hh:mm") ' Format date correctly
Debug.Print "sStartTime after formatting: " & sStartTime
This is what output I usually get:
sStartTime after reading: 05/10/11 23:59
sStartTime after formatting: 10/05/11 23:59
But sometimes it swaps the day and year even:
sStartTime after reading: 14/07/11 23:59
sStartTime after formatting: 11/07/14 23:59
Then CDate completely stuffs things around, converting dates to such things as 1931...any help converting a date string to a date object would be greatly appreciated.
===============
Edit: Should probably have mentioned this in the initial post. The idea behind reading the string, is to convert to a Date object so that I can create a calendar appointment.
Current I use this;
dStartTime = CDate(sStartTime)
Which I think is the problem line, sStartTime = "29/09/11 23:00" (dd/mm/yy hh:mm) and dStartTime = "11/9/2029 11:00:00 PM"
So obviously there's some conversion issues going on there, but I have no idea what format I'm meant to be feeding to the CDate function in order to turn
29/09/11 23:00 into the equivalent date object.
Format(sStartTime, "dd/mm/yy hh:mm") cannot correctly work since sStartTime is a string, NOT a date.
You have to do some extra work to get a correctly typed Date, like
dStartTime= DateSerial(Mid(sStartTime,10,2),Mid(sStartTime,7,2),Mid(sStartTime,4,2)) + TimeSerial(...)
etc...
THEN, you will be able to apply your Format function correctly.

Format function vba changing date

The format function in vba is changing the date. e.g for format("3/12/2009","DD/MM/YYYY"), the function returns 12/03/2009 where "3/12/2009" is what excel vba reads from a cell that has the value 12-Mar-2009 and the format as "dd-mmm-yyyy"
No it's not.
If a date-as-string is passed to the Format function, it will parse it using current regional settings. Your settings are obviously MM/DD/YYYY which is default for USA. Nothing prevents Excel from displaying a date as DD/MM/YYYY if set manually, but by default it would display MM/DD/YYYY.
To do: Stop reading dates as strings. Read them as dates.
dim d as date
d = activecell.value
Had few times problem myself where VBA in Access reades most dates as europian but some as USA:
This DOES NOT work properly:
myRs.FindFirst ("Date =#" & myDate & "#")
This works:
myRs.FindFirst ("Date =#" & Format(myDate, "Long Date") & "#")
The long date (eg 01 January 2012) clearly makes the difference between month and day