Date format change SSRS - sql

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)

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 value returning in a different format on the first of each month

This question is related to this one, however I thought I'd create a new post since it's not the exact same issue, and the ideas on it were just being repeated.
I have a selection formula for my Crystal Report. It's supposed to select data where the Stage field in one table is 6, and the PaymentDate field in another is less than, or equal to, the value of the DateTimePicker control.
The code that I have below is working fine for most of the dates. However, say for example I have the following data in the database:
Sales_Headers.Stage = 6
Sales_Lines.PaymentDate = 28/01/2017 (January 28th, 2017)
When choosing a date of January 26th, up to January 31st, the data is only retrieved when the date is 28th or higher. However, if I then select a date of 1st February (Or the 1st of any month to be precise), it is returning the date as 02/01/2017, or, 2nd January 2017, so the data isn't shown.
Why is it changing for only the 1st of each month? All other dates are being read correctly, as dd/MM/yyyy, but on the first, it's using the MM portion as the dd portion.
I've tried:
Dim dateTo As Date = dtpCRTo.Value.AddDays(1).Date
dateTo = Format(dateTo, "dd/MM/yyyy")
If cmbCRSupplier.Value = "" Then
selectionFormula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} < #" & dateTo & "#"
Then I tried this in the form_Load event, as well as in the Value_Changed event of the DateTimePicker:
Dim dateFormat As String
dt.Format = DateTimePickerFormat.Custom
dt.CustomFormat = "dd/MM/yyyy"
dateFormat = dt.Text
The final thing I tried was just to have no formatting code, and just using:
If cmbCRSupplier.Value = "" Then
selectionformula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} <= #" & dtpCRTo.Value.Date & "#"
But it was the same result for all of them.
As well as the method #Siva was talking about (Always a good way, that way you can create the formula there and use the syntax that it's looking for), there is the way you're trying, to do it in VB.NET.
As I just mentioned, the syntax is important, and this is what is causing your issue.
I've not seen anyone try to use DateTimes in this method before, using `#value#.
The correct way to format dates into a RecordSelectionFormula (again, this is something you'll have seen if you'd have created it in Crystal itself), is to DATE(yyyy, MM, dd).
So, the correct way to syntax this is to use:
selectionFormula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} <= DATE(" & _
dtpCRTo.Value.Date.Year & "," & dtpCRTo.Value.Date.Month & "," & dtpCRTo.Value.Date.Day & ")"
Use this method to insert your dates into selection formulas in the future, that way you can't get it wrong.
If this doesn't work, then you need to check your Region and Local Date/Time settings in Control Panel, to ensure they're set correctly.

Converting a UserResponse with ToDate returning only January in Business Objects

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

Rounding dates to the day in an SQL query

I am stuck with something. I am trying to take a long column of dates of which are formated to show also hours and minutes and run a Group query to paste values at the date level without acknowledging the differences in hours and minutes.. Unfortunately I have no clue how to start. The code i put together so far which returns each grouped date with the hour and minutes is as follows:
st_sql = "INSERT INTO [tblSearchEngine03] ([Date])" & _
"SELECT [tblSearchEngine02].[Date]" & _
"FROM [tblSearchEngine02]" & _
"GROUP BY [tblSearchEngine02].[Date]" & _
"ORDER BY [tblSearchEngine02].[Date]"
Application.DoCmd.RunSQL (st_sql)
Im not sure the best way to truncate the date on table "tblSearchEngine02"..
Focus on the SELECT piece first. You can use DateValue() for your Date field values. Start with this as a new query in the Access query designer:
SELECT DateValue(se02.Date)
FROM tblSearchEngine02 AS se02
GROUP BY se02.Date
ORDER BY se02.Date
Or you could use DISTINCT instead of GROUP BY:
SELECT DISTINCT DateValue(se02.Date)
FROM tblSearchEngine02 AS se02
ORDER BY se02.Date
After you have the SELECT working correctly, you can convert it to an INSERT query (the Access query designer calls it an "append" query).
And when you later build the same statement in your VBA code, include Debug.Print st_sql so that you can view the completed statement text in the Immediate window and make sure it is what you expected. (You can use Ctrl+g to go to the Immediate window.)
One way of doing this is to format the date/time as a date string. If you use YYYY/MM/DD it will sort properly. Otherwise you can convert the date/time to an int to trim off the time and then convert back to a date/time type.
Here is an example of formatting as string:
Format([tblSearchEngine02].[Date], "yyyy/mm/dd")
Here is an exmple of converting to get to a date (the end result will be a date/time data type so it might render as 03/16/2014 00:00 depending on your locale info)
CDate(CInt([tblSearchEngine02].[Date]))
Access stores its dates as floating point numbers where the integer part is the number of days since Jan 1, 1900 and the fractional part is the fraction of the day (time of day). Access is quite happy to treat these dates as numbers without doing any conversions, so:
fix([tblSearchEngine02].[Date])
will trim the fractional part of the day and set the time back to midnight and allow you to group by day.

Date Manipulation as text field

I am using Access 2010 as a front-end to a database on SQL Server 2008. I have a date field which is stored as a nvarchar(50). I have the following value in the text field DateHr 12/04/11 16:49:23 , which should translate to April 11, 2012 4:49 PM (As is the date and time the record was created.).
I cannot change the datatype of the field to DateTime as it messes up the dates even more (Ex. 12/4/2011 4:49:23 PM). I cannot change the way the record is entered.
I need to display this field in the format "mm/dd/yy" and be able to do where clause in this format.
I have tried the following just to see if it displaying correctly but dtDate is displaying 11/12/04:
Select (Format(CDate([DateHr]),"yy/mm/dd")) as dtDate
If you need to carry out the conversion in access then you can try either of these:
Select Mid([DateHr],4,2) & "/" & Mid([DateHr],1,2) & "/" & Mid([DateHr],7,2) as dtDate
This I think will give you the date in a string format, otherwise you could try the following to get it in a valid date format:
Select Format(DateSerial(Mid([DateHr],7,2),Mid([DateHr],4,2),Mid([DateHr],1,2)),"MM/DD/YYYY") as dtDate
Create a SQL Server view to expand the year component of the date text to 4 digits. Then SELECT CDate([DateHr]) AS dtDate FROM YourView should work from the Access side. However it might be better still to have the view cast the date text to an actual date type ... then you could use it directly from Access without the need for CDate().