SSRS Converting int to date and handling #Error - reporting-services

I'm a bit of an SSRS newbie and I'm having some trouble converting an Int to a Date in SSRS. The publication_date.Value is fed from a proprietary database as an Int type (so we unfortunately have no control over that) in the format 20160519.
The following works splendidly when there is a value present:
=CDate(Left(Fields!publication_date.Value, 4) + "-" + Mid(Fields!publication_date.Value, 5, 2) + "-" + Right(Fields!publication_date.Value, 2))
However, when there is a null it returns an #Error, which I'd like to suppress. I've tried using an IIf statement but without success.
=Iif(Fields!publication_date.Value is nothing,"", CDate(Left(Fields!publication_date.Value, 4) + "-" + Mid(Fields!publication_date.Value, 5, 2) + "-" + Right(Fields!publication_date.Value, 2)))
I've been digging around for other solutions but yet to find anything that works. Please help!
Edited - thanks for the duplicate article suggestion, however I need to convert an Int to a Date and handle the errors, not a Date to an Int.

I had the same issue,
As per your example, do one thing.
Keep you expression same like below,
=CDate(Left(Fields!publication_date.Value, 4) + "-" + Mid(Fields!publication_date.Value, 5, 2) + "-" + Right(Fields!publication_date.Value, 2))
Now, tricky part would be, in textbox visibility use below expression.
=IIf(IsNothing(Fields!publication_date.Value),True,False)
It will definitely work. Let me know if you face any issue. Thanks.

Related

SSIS: string(varchar) to date conversion

I need to achieve the date format yyyy-mm-dd conversion on SSIS
I tried the below code but I keep getting error:
year and posting_period is a varchar(50) on my source data.
Sample data on my source:
year = 2014
posting_period = always 2 digit
What did I miss in my codes below?
(DT_DBDATE)((DT_WSTR,4)year + "-" + (DT_WSTR,2)posting_period + "-" + "01")
You need to set the variable's data type to datetime; encapsulate the string in parenthesis; and cast it as DT_DATE:
(DT_DATE)(#[User::Year] + "-" + #[User::posting_period] + "-01")
To my knowledge, you cannot specify how the datetime is displayed. So, this will evaluate to MM/DD/YYYY, which is fine. You should not worry how the date is formatted in the database. You should only care about how it is displayed in the reporting tool, which typically allows you to convert or cast the date in many different ways.

SSIS DT_DBDateTimeStamp to DD/MM/YYYY without script task (derived column)

I'm trying to construct an SSIS package where one of the columns is a DT_DBDataTimeStamp format.
My problems is that I have to convert this to a DD/MM/YYYY format and I can't use a C# script.
I'm trying to use a derived column by having difficulty in getting the correct format.
I've tried Convert(varchar, Date_Column, 103) - doesn't work.
I've tried DAY(Date_Column) + MONTH(Date_Column) + YEAR(Date_Column) - doesn't work.
Anybody know how I can do this?
It would need to be varchar if you want this (actually, I will use nvarchar for simplicity):
right("0" + (DT_WSTR,2) day(Date_Column), 2) + "/" +
right("0" + (DT_WSTR,2) month(Date_Column) , 2) + "/" +
(DT_WSTR,4)year(Date_Column)

SSIS converting Time Stamp format using derived column

I have a timestamp in a text file that looks like this: 7/2/2013 17:40:22
I need to convert it to this: 2013-07-02 17:40:22.913
Using a derived column, I tried this: (DT_DBTIMESTAMP)TIME_STAMP
and
(DT_DBTIMESTAMP)(SUBSTRING(TIME_STAMP,1,4) + "-" +
SUBSTRING(TIME_STAMP,5,2) + "-" +
SUBSTRING(TIME_STAMP,7,2))
But I am getting an error: Conversion between types DT_STR and DT_DBTIMESTAMP is not supported.
Thanks!
If you must go the route of casts, let me know and I'll edit this answer to reflect it. Otherwise, simplify your life and fix your Flat File Connection Manager. By default, it's going to assume every column coming in is varchar(50). This gets tricky with dates and times because well known formats that should just convert require far too much expression garbage to make them work.
In your case, change the type to DT_DATE I know you'd think it's for dealing date only but that'd be DT_DBDATE.
By simply changing to that data type, I was able to import your value without issue.
using token will do ... assume your timestamp is in a string(WSTR) format
TOKEN([Order Date],"/",3) + "-" + TOKEN([Order Date],"/",1) + "-" + TOKEN([Order Date],"/",2)

How to convert string in format yyyyMMdd to date using SSIS expression?

I have a date 20130131 in csv which I'm trying to convert to 2013-13-01 using Derived Column in SSIS. I have used this expression but it does not seem to work.
(DT_DBTIMESTAMP)(SUBSTRING(DATE_DECISION_TO_REFER,1,2) + "-" + SUBSTRING(DATE_DECISION_TO_REFER,3,2) + "-" + SUBSTRING(DATE_DECISION_TO_REFER,5,2))
How do I rewrite this expression to produce the correct output with a value of data type DT_DBTIMESTAMP?
Fast Parse is a much more elegant solution
Add a Data Conversion Data Flow Component.
Right click, Show Advanced Editor...
Goto Input and Output Properties
Expand Data Conversion Output and click on the date column
Set FastParse to True in the Custom Properties
Make sure the data type is set to database date [DT_DBDATE]
Run the ssis package again the yyyymmdd dates should flow smoothly from nvarchar to date
As per Kyle Hale suggestion, below is my answer
SUBSTRING([DATE_DECISION_TO_REFER],1,4) + "-" +
SUBSTRING([DATE_DECISION_TO_REFER],5,2) + "-" + SUBSTRING([DATE_DECISION_TO_REFER],7,2)
To educate you so that you will never face this issue again, this is how expression works
Get 4 characters from DATE_DECISION_TO_REFER starting at position 1, add a dash,
get 2 characters from DATE_DECISION_TO_REFER starting at position 5,
add a dash then add 2 characters from DATE_DECISION_TO_REFER starting at position 7.
Hope this will help.
Expression:
You should use the expression to format the date to yyyy-MM-dd like shown below. This expression will format the incoming value 20130131 to 2013-01-31 and then will convert to appropriate data type. The expression uses the incoming column named Column0.
(DT_DBTIMESTAMP)(SUBSTRING(Column0,1,4) + "-" + SUBSTRING(Column0,5,2) + "-" + SUBSTRING(Column0,7,2))
Issue:
Your expression is taking the value 20130131 and then incorrectly converting it to the value 20-13-01. This is an invalid format for dates and hence the expression is failing to type cast it to DT_DBTIMESTAMP data type.

SSIS how to build unique filename in Expression

I would like to create random unique filename in SSIS using expression builder for Flat file connection Manager - ConnectionString.
Should be like: "Custom name" + "unique part" + "csv"
Actualy a timestamp in miliseconds would do.
Any idea?
I suggest you using the expression design to do that.
Here Im using the ExecutionGuid to generate the value. It may not be the best idea because the result is quite big but you can use datetime functions as well to create something on the format you desire:
It's a litte convoluted thanks to all the REPLACEs, but you can use:
"Custom Name " + SUBSTRING(REPLACE(REPLACE(REPLACE((DT_WSTR, 50)(GETDATE()), "-",""), ".", ""), ":",""), 1, 18) + ".csv"
as a starting point. This will return:
Custom Name 20120529 133526359.csv
as a possible filename. It's not 100% guaranteed unique, and it's obviously not random, but I think it's all that can be done just in an Expression. Alternatively, you can use a Script Task to generate a more random string using C#/VB.NET, write that into a variable and use the variable in your Expression.
string.Format("Custom name{0}.csv", Path.GetRandomFileName());
or
string.Format("Custom name{0}.csv", DateTime.Now.ToString("yyyyMMddhhmmssfff"));
This will be to get unique date time..
" + (DT_WSTR, 30)(DT_DBDATE)GETDATE() + "_" +
(DT_WSTR, 05) DATEPART("Hh",GETDATE()) +
(DT_WSTR, 05) DATEPART("Mi",GETDATE()) +
(DT_WSTR, 05) DATEPART("Ss",GETDATE()) +
".csv"
I found that it was easy to find how to create the expression but difficult to find instructions on how to set your output filename to use this expression. It is not a straightforward process. Here is an answer to this problem. I hope this helps somebody.
https://blogs.msdn.microsoft.com/sqlgardner/2015/06/18/ssis-tip-using-a-file-path-parameter-for-multiple-flat-files/#comment-1755