Format Date Time Exception in SSRS - sql-server

I have built a SSRS report which combines two int columns of data (i.e.: 6 & 2017) to form a valid month and year result in a column of the SSRS report (i.e.: June 2017) using the following statement: Format(CDate(Fields!MonthEarnedId.Value & "/" & Fields!YearEarned.Value)
That works as desired, however some of the int values for the month have a value of 13 which is supposed to represent all months of the year so that the required format for these values in the report is such as "All 2017". I thought an IIF statement such as the following would specify the necessary formating for the exclusion, however the rows with a month value of 13 simply display #error. Any suggestions would be greatly appreciated!
=iif(Fields!MonthEarnedId.Value < 13, Format(CDate(Fields!MonthEarnedId.Value & "/" & Fields!YearEarned.Value), "MMM yyyy"), "All " & Fields!YearEarned.Value)

The issue in expression is CDate() when the 13 occurs. As per the design in SSRS, IIF evaluates TRUE and FALSE both and in your case when the 13 is selected as MonthEarnedId then CDate() tries to convert 13/2017 in MMM yyyy format which is invalid and throws #Error.
I would suggest you to convert the int into month names in your SQL query and bring the data to just concatenate in SSRS but if you strictly have to do it in SSRS then below is the expression you have to put in.
=SWITCH(Fields!MonthEarnedId.Value=1,"Jan"
,Fields!MonthEarnedId.Value=2,"Feb"
,Fields!MonthEarnedId.Value=3,"Mar"
,Fields!MonthEarnedId.Value=4,"Apr"
,Fields!MonthEarnedId.Value=5,"May"
,Fields!MonthEarnedId.Value=6,"Jun"
,Fields!MonthEarnedId.Value=7,"Jul"
,Fields!MonthEarnedId.Value=8,"Aug"
,Fields!MonthEarnedId.Value=9,"Sep"
,Fields!MonthEarnedId.Value=10,"Oct"
,Fields!MonthEarnedId.Value=11,"Nov"
,Fields!MonthEarnedId.Value=12,"Dec"
,Fields!MonthEarnedId.Value=13,"All"
) & " " & CStr(Fields!YearEarned.Value.Value)

You are casting that field type to a date variant to apply formatting in doing so, you are also changing the data type of the column from string to datetime. The error occurs when the renderer attempts to cast the string value "All 2017" into a corresponding date time. To get around this you need to cast it back to a string subtype using CStr() or some other way.
=iif(Fields!MonthEarnedId.Value < 13, CStr(Format(CDate(Fields!MonthEarnedId.Value & "/" & Fields!YearEarned.Value), "MMM yyyy")), "All " & Fields!YearEarned.Value)

Related

SSIS DT_STR to DT_DBDATE using Derived column

I have a fixed format .txt file which has a date field. This field may have a date or be blank. I have wracked my brains to get this field to convert from DT_STR to DT_DBDATE in the Derived Column transformation. I have tried multiple scenarios:
[CHNG_DT] == "" ? ISNULL((DT_DBDATE)[CHNG_DT] ) : (DT_DBDATE)SUBSTRING([CHNG_DT], 1, 4)) + "-" + (DB_DBDATE)(SUBSTRING(CHNG_DT], 6, 2)) + "-" + (DB_DBDATE)(SUBSTRING([CHNG_DT], 9, 2))
[CHNG_DT] == "" ? NULL(DB_DBDATE) : (DB_DBDATE)[CHNG_DT]
ISNULL([CHNG_DT]) ? NULL(DT_DBDATE) : (DT_DBDATE)((DT_STR, 10, 1252)[CHNG_DT])
ISNULL(CHNG_DT) ? NULL(DT_DBDATE) : (DT_DBDATE)((DT_STR,10,1252)CHNG_DT)
LEFT([CHNG_DT], 10) == " " ? ISNULL((DT_DBDATE)[CHNG_DT]) : (DT_DBDATE)[CHNG_DT]
(DT_DBDATE)CHNG_DT
First of all, when you are looking to convert values from DT_STR to DT_DBDATE, you should make sure that those values are stored in the yyyy-MM-dd format. You can refer to the following official documentation to learn more:
Converting Between Strings and Date/Time Data Types in SSIS
The second suggestion is to use the LTRIM() and RTRIM() functions to make sure that there are no white blanks in the beginning and end of the date values. As an example:
LTRIM(RTRIM(REPLACENULL([CHNG_DT],""))) == "" ? NULL(DT_DBDATE) : (DT_DBDATE)LTRIM(RTRIM([CHNG_DT]))
If date values are stored in a different format, you should write an expression that changes the format before converting it to DT_DBDATE. As an example:
How to convert string in format dd.mm.yyyy to date using SSIS expression?
Other helpful answers:
SSIS Source Format Implicit Conversion for Datetime
Convert DDMonYY and time to datetime column in SSIS package (Derived Column)
The year function doesn't support dt_wstr

How do I get one week list from SQL Server database by using a statement in VB.NET?

I would like to know if it is possible to set this?
This is from SQL Server.
My logical explanation is
VB.NET:
If "Date_Needed between `getdate()` And `dateadd(day,7,getdate())` "
Then
`DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.Red`
End If
or
If Date_needed = (1 weeklist) from (Today's Date)
Then
`DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.Red`
End If
Date_needed is a column value fetched from SQL Server.
If the system reads the datagridview with today's date to one week list the selected rows in datagridview will turn red forecolor highlight.
Select *
From PRF_Form
Where Date_Needed Between getdate() And Dateadd(day, 7, getdate())
I would have added a column to the returned result set to indicate if the row falls into the 'NeededDate' selection. Let my database do the heavy lifting there, and just rely on the VB.NET side of things to style based upon the values returned from the query.
But, first, I want to point out an issue you made in your own answer to this question. The "&" in VB.NET is a string concatenation operator not a logical AND operation. & will implicitly cast objects to string when used.
So, this line in your response
Date.Now & DateAdd(intervaltype, days, secondDate)
would have generated a string that looked something like: April 13, 2019April 5,2019
Which is not good for date comparisons. Additionally, it would not throw a compiler error, because it will automatically convert the date object on the left side of the compare operator to string, since the value on the right side was a string.
It's been stated before, but you do need all of the elements in an actual DATE type in order to compare dates.
So try something like this
Dim startdate As Date
If Not Date.TryParse(DateString, startDate) Then startDate = Date.Now
Dim days As Double = 7
Dim secondDate As Date
secondDate = DateAdd(DateInterval.Day, days, startdate)
For i = 0 To dt.Rows.Count - 1
Dim checkDate As Date = Date.Parse(dt.Rows(i)(5).ToString)
If checkDate <= Date.Now AndAlso checkDate >= secondDate Then
DataGridView1.Rows(i).DefaultCellStyle.ForeColor = Color.Red
End If
Next
Note the "AndAlso" operator. This is similar to an And operator, but will only check the compare option to the right of it if the compare operator to the left of the AndAlso returns true.

Formatting YYYYMM to MMMYYYY in SSRS

I would like to to format one of my data fields to be in the date format of MMM YYYY. For example, if "201209" is being returned, then I would like it to be displayed as Sep 2012. On the SQL side I am using a CAST to only look at the Year and Month, normally this field would look like "20120914". Here is what I used in the procedure:
cast(left(cast(TransactionDateKey as varchar(100)), 6) as int)
Within SSRS, I use the following code:
=Format(DateValue(MonthName(Right(Fields!Month.Value, 2))
+ "," +
Left(Fields!Month.Value,4)), "Y")
however the output is "September 2012", how would I go about get the abbreviation "Sep", instead of the full month name?
Another Option is Format() in SQL Server
Select Format(CONVERT (date,convert(char(8),TransactionDateKey )),'MMM yyyy')
For example
Select Format(CONVERT (date,convert(char(8),20120914 )),'MMM yyyy')
-- Returns Sep 2012
I should note that Format() is not known for its performance, but does offer some nice features
If you are returning an actual datetime field in your dataset - which is what your TransactionDateKey appears to be - you can handle the formatting completely in an SSRS expression using format:
=format(Fields!TransactionDateKey.Value,"MMM yyyy")
If you are returning your yyyyMM for grouping purposes, there is nothing stopping you grouping on a datetime value of the first of the month for TransactionDateKey within your SQL:
select dateadd(m,datediff(m,0,TransactionDateKey),0) as FirstDayOfTheMonth
If you absolutely need to return a varchar in the format yyyyMM, you can convert it to MMM yyyy, though you first need to convert it to a date in your expression - slashes and all - before SSRS will play ball:
=format(cdate(left(Fields!Month.Value,4) & "/" & right(Fields!Month.Value,2) & "/01"), "MMM yyyy")
In SSRS you can use:
=StrConv(LEFT(
MONTHNAME(REPLACE(RIGHT(Fields!Month.Value,2),"0","")),3
),vbProperCase,NOTHING) & " " & LEFT(Fields!Month.Value,4)
Which returns Sep 2012 for 201209.
Let me know if this helps.

MS Access 2010 Form filter doesn't work with last year

I have a form in which I have a subform linked to a table that contains various columns and rows, one of which is a Date Created column.
In this form there is a button that filters the subform so that it shows all records starting from half a year ago, here is the code for the variable
startDate_S = Replace(Format(DateAdd("m", -6, Date), "mm/dd/yyyy"), ".", "/")
And the filter itself
ProbSub.Form.Filter = "[Date Created] > #" & startDate_S & "#"
The DateAdd() sets the current date back by 6 months, Format() and Replace() change the date format to the american one instead of the local one (MS Access filters only use this standard).
I use a messagebox to see that the formatting is correct, it returns "12/06/2015".
But it does not return any of the rows (I have 3 with the Date Created values of "06/03/2016"). Through debugging I have found that this occurs only when the starting date is a year ago or later, e.g., when startDate_S is "01/01/2016" or higher it works, but when it is "12/31/2015" or lower it does not.
There is need for Replace:
startDate_S = Format(DateAdd("m", -6, Date), "mm\/dd\/yyyy")
Your filter should work if [Date Created] is of data type Date, but not if is Text. If so:
ProbSub.Form.Filter = "DateValue([Date Created]) > #" & startDate_S & "#"

SSIS - Converting unformatted strings dates to YYYY-MM-DD

I'm trying to convert the string column [mydate] to a date, and I would like to use the Derived Column Transformation. The problem is this that my dates are like '1/10/2015', '1/1/2015', '11/1/2015' and '11/9/2015'. So the format can change between D/MM/YYYY, D/M/YYYY, DD/M/YYYY, and DD/MM/YYYY.
Can you guide me in creating the expression for the Derived Column Transformation?
I tried to use something like this:
(DT_DATE)(ISNULL([mydate]) == FALSE ? (RIGHT([mydate],4) + "-" + "2" + "-" + LEFT([mydate],1)) : [mydate])
This task is quite tricky since we're not only dealing with unformatted String dates but they are also not in the region netural YYYY-MM-DD format.
To fix this, we can use the follwing expression for the Derived Column:
RIGHT([mydate],4) + "/" + SUBSTRING([mydate],FINDSTRING([mydate],"/",1) + 1,FINDSTRING([mydate],"/",2) - FINDSTRING([mydate],"/",1) - 1) + "/" + SUBSTRING([mydate],1,FINDSTRING([mydate],"/",1) - 1)
The expression is quite long but what it is doing is taking the values (month, day, year) between the forward slashes / and concatenating them into a format that resembles YYYY/MM/DD that can then be converted in SSIS using a Data Conversion transformation. This avoids the error of dealing with the change in length in dates like 1/1/2000 and 10/10/2000.
The output of the derived column was named YYYYMMDD and this value was then passed into a Data Conversion transformation that has output Date Converted YYYYMMDD as seen below.
The Data Conversion task is simply doing the follwing:
You can use Replace to solve this
Replace(ColumnName,"/","-")

Resources