Data Conversion In ETL SSIS - sql-server

I'm working in ETL process where I have a column in a table varchar with data as 28JUN1952.
This will always be in this format i.e. 2 digits for date, 3 letters for months and 4 numbers for years.
I was able to convert this into a date column by I need this to come in yyyyMMdd format i.e. 28JUN1952 become 19520628. I was able split the years months and date, but unable to add a padding in the month if it's a 1 digit month for e.g. Jan or Feb.
Is there any other way to convert the data format or any other alternative to add a padding?
I need the conversion in SSIS only.

The easiest route will be to add a Script Transformation.
On the Input and Output tab, expand the Output 0, select Output Columns, click Add Column. 2. Rename Column to something like ccyymmdd and change the data type. I'm not sure what your target type is (int32, string?) but I'll assume string as I'm lazy
On the Input Columns tab, select the string date source column (assuming it's called srcDate)
Inside the script task, assuming C#, use code like this
Inside script task
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
DateTime x = DateTime.MinDate;
if (DateTime.TryParse(Row.srcDate, out x))
{
Row.ccyymmdd = x.ToString("yyyyMMdd");
}
else
{
// Handle NULL values however you wish
// Assign "known bad value"
// Row.ccyymmdd = "99991231";
// Or set the null property to true (preferred)
Row.ccyymmdd_IsNull = true;
}
}

Try this:
declare #d varchar(20)='28JAN1952';
SELECT replace(CONVERT(date,RIGHT(#d,4)+SUBSTRING(#d,3,3)+LEFT(#d,2)),'-','')

Related

wrong Dates values in ssis

I am working with ssis. I have a column that contains date in format integer YYYYMMDD: For example to day I get 20190214.I am using derived column to convert it in format date YYYY-MM-DD in my case for example 2019-02-14
But sometimes I receive wrong values for example 20188101. How could I test that I have good values to be converted to date?
There are 3 methods to achieve that
(1) Using Another Derived Column
Beside of the first Derived COlumn, you can add another derived column with the following expression:
(DT_DATE)(LEFT([DateColumn],4) + "-" + SUBSTRING([DateColumn],5,2) + "-" + RIGHT([DateColumn],2))
If the date value is incorrect redirect the bad from Error Output configuration , and you can handle these bad values from the Error Output.
Helpful Links
USING THE ERROR OUTPUT OF THE DERIVED COLUMN
How to prevent CAST errors on SSIS?
(2) Using a Script Component
Add a script component to check if the value is correct, and add a Output Column of type i.e. OutDateColumn, use a similar code (VB.NET)
IF Not Row.DateColumn_IsNULL Then
dim dtDate as DateTime
If DateTime.TryParseExact(Row.DateColumn,"yyyyMMdd",System.Globalization.InvariantCulture,System.Globalization.DateTimeStyles.None , dtDate ) Then
Row.outDateColumn = dtDate.ToString("yyyy-MM-dd")
Else
Row.outDateColumn_IsNull = True
End If
Else
Row.outDateColumn_IsNull = True
End If
(3) Add a Data Conversion Transformation
After the derived column Transformation, add a Data COnversion Transformation and try to convert the Date Derived Column you add to DT_DATE, if conversion failed, than handle the bad values from Error Output as explained in the first method.

SSRS date range parameter

I have a report in SSRS 2016 that contains one SP which has a date field called startdate. I'd like to create a parameter where the user can select between two ranges: startdate >='2017'or startdate < '2017'. This seems like it should be simple, but I can't see to find an easy way to do this.
I've tried to create a parameter in SSRS where I chose "Specify Values" and manually added these values but I get an error that the "expression references the parameter "startdate" which does not exist in the Parameters collection.
I'm sure I can create this by creating a stored procedure with these dates, but that seems like more work than is needed.
Does anyone know of a better way to do this?
If you are looking to have a parameter that has two options, 0 - Before 2017, and 1 - After 2017 then you should just create a Date parameter which has two options, Before 1/1/2017 in the label field with a 0 in the value field and After 1/1/17 in the label field with a 1 in the value field. Then in your report you just have to filter your data based upon the 1 or 0 value.
For example:
DECLARE #DateFilter INT = 1
IF ( #DateFilter = 0 )
BEGIN
SELECT * FROM dbo.MyTable t
WHERE t.DateFilter < '1/1/17'
END
IF ( #DateFilter = 1 )
BEGIN
SELECT * FROM dbo.MyTable t
WHERE t.DateFilter >='1/1/17'
END
I don't know why you would filter your data this way. I recommend to use Larnu's suggestion that you have two parameters, a Start Date and an End Date. Otherwise this could return a lot of rows for the second option as time goes on.
Add a couple of parameters in SSRS say, FromDate and ToDate and make its data type as Date/Time.
You can specify the default values for these parameters using DateAdd functions and samples give below:
Today:
=Today()
last week from today:
=DateAdd(DateInterval.Day, -7,Today())
You can add/reduce year, quarter month etc. in a similar way as shown below. Just change the number to the required length
=DateAdd(DateInterval.Year,-1,Today())
=DateAdd(DateInterval.Quarter,-1,Today())
=DateAdd(DateInterval.Month,-1,Today())
=DateAdd(DateInterval.DayOfYear,-1,Today())
=DateAdd(DateInterval.WeekOfYear,-1,Today())
=DateAdd(DateInterval.WeekDay,-1,Today())
=DateAdd(DateInterval.Hour,-1,Today())
=DateAdd(DateInterval.Minute,-1,Today())
=DateAdd(DateInterval.Second,-1,Today())

Date and/or time from character string error message

SELECT communication.*
,employeedetails.resourcename
FROM communication
,employeedetails
WHERE employeedetails.employeenumber = communication.employeenumber
AND communication.project = employeedetails.projectname
ORDER BY
CONVERT(DATETIME ,communication.month ,5)
I am getting the following error message:
"Conversion failed when converting date and/or time from character
string"
Kindly help me out
It is not easy having no info regarding the data tables involved!
However if names are meaningful, how can you convert a month to a datetime?
If it is existing try this:
SELECT communication.*, employeedetails.resourcename
FROM communication, employeedetails
WHERE employeedetails.employeenumber = communication.employeenumber and communication.project = employeedetails.projectname
ORDER BY communication.day, communication.month, communication.year --due to style 5
even if I would suggest to go for style 12
The datatype of communication.month is presumably a character type. If this holds "January" for January etc. then SQL has no way of converting that character to a datetime. You would need to append a string such that SQL could parse it as a datetime.
e.g. ORDER BY CONVERT(datetime,communication.month + '01 2000',5).
However, storing the communication date as a datetime in the first place would be better...

SQL DATETIME Insert from Excel?

So im having a rather strange problem, I have a Column (lets say Column A) in excel that has data that looks like this:
4/11/2015 10:14
I have a bunch of other columns, but anyways in my SQL Insert statement within excel, the data (when copying out) looks like this:
42105.4561921296
The ="INSERT INTO TABLE VALUES ('"&A1&"', Etc....)" is in the data format of "general" and the Date column is in the format of "Custom" where there is a M/DD/YYYY MM/HH type within.
The SQL Column is of the data type DATETIME, so it of course doesn't accept the weird number it gets.
Any ideas? changing the format of the "SQL INSERT" column doesn't change the results.
You are right - Excel formats only changes the way the numbers are displayed, not the underlying value of the cell. In this case, the value of the cell is an excel date-time value, which is the # of days since 1/1/1900, with decimals for the time.
I'd recommend using Excel's TEXT function to convert Excel's numeric date-time value to a text string that can be inserted into SQL:
Instead of:
INSERT INTO TABLE VALUES ('"&A1&"', Etc....)"
Try:
INSERT INTO TABLE VALUES ('"&TEXT(A1,"YYYY-MM-DD HH:MM")&"', Etc...)"
The best format to insert a date time into a datetime column is to present the date and time as YYYY-MM-DD Minute:Second or 2015-04-15 12:52
so to insert a datetime from Excel you can use this set of Excel functions:
(where A1 contains the datetime to be saved)
=YEAR(A1)&"-"&RIGHT("00"&MONTH(A1),2)&"-"&RIGHT("00"&DAY(A1),2)&" "&RIGHT("00"&HOUR(A1),2)&":"&RIGHT("00"&MINUTE(A1),2)&":"&RIGHT("00"&SECOND(A1),2)

Run SSIS Package skipping Invalid dates

I've a DWH Table which has Date Value stored as int.Now I wanna get all records for a day which are active on a day with conditions as start_date<=#date and End_Date>#DATE
for almost close to 2 years.
I've used a for loop and 3 variables where in hardcoded FROM_DATE and TO_DATE as 20130101 and 20150107 and the 3 variable VAR_DATE initially set TO_DATE and decreasing by 1 until it reaches FROM_DATE.
But after reaching 20150101...It's starting to insert values for 20150100,20150099 and so on..
Is there a way where in I can check to see if the VAR_DATE column being used is actually a valid date(by convert and comparing on the go) or any possible way of using a result set..
Thanks,
Vijay
Use a script to convert the integer into a string. Then parse it into a date object like so:
DateTime parsedDate;
bool parseResult = DateTime.TryParseExact(dateValue, "yyyyMMdd", null, DateTimeStyles.None, out parsedDate)
Then parseResult will be true if it's a valid date.

Resources