SQL Server Development Bookmark and Share   
 index > SQL Server Integration Services > TRY CATCH FOR INVALID DATES IN SCRIPT COMPONENT (Transformation) IN DATA FLOW TASK
 

TRY CATCH FOR INVALID DATES IN SCRIPT COMPONENT (Transformation) IN DATA FLOW TASK

What logic can I use in my script component to implement Try / Catch invalid dates and replace them with some default value "1/1/1900"

Please include links to code/tutorials or sample code. The sample below does not work for the invalid date "01/01/0200"

Howshould I split the data source to log/redirect to(error input table)these rows with invalid dates? I only want to redirect the rows with invalid dates not the entire batch.

' Microsoft SQL Server Integration Services user script component

' This is your new script component in Microsoft Visual Basic .NET

' ScriptMain is the entrypoint class for script components

Imports System

Imports System.Data

Imports System.Math

Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper

Imports Microsoft.SqlServer.Dts.Runtime.Wrapper

Public Class ScriptMain

Inherits UserComponent

Dim FirstDate As Date = New Date(1900, 1, 1)

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

'

If (Not Row.STARTBILLDATE_IsNull OR Row.STARTBILLDATE < FirstDate)Then FixUpDate(Row.STARTBILLDATE)

'

End Sub

Private Sub FixUpDate(ByRef D As Date)

If D < FirstDate Then

D = FirstDate

End If

End Sub

End Class

boston_sql92  Sunday, September 28, 2008 6:49 PM
Here is a small C# console app that wrote in VS C# 2005. Hope you can use the same code and mould it for script task in SSIS


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string dt ;
dt = "09/09/2008";
bool bl = IsDate(dt);

Console.WriteLine(dt+":Is date input");
Console.WriteLine(bl + ":Is date output");


dt = "09/100/2008";
bl = IsDate(dt);

Console.WriteLine(dt + ":Is date input");
Console.WriteLine(bl + ":Is date output");

}
public static bool IsDate(string input)
{
try
{
DateTime.Parse(input);
return true;
}
catch
{
return false;
}
}

}
}

Ashwani Roy - IMGROUP  Sunday, September 28, 2008 7:47 PM

I found the isdate() T-SQL function invaluable "catching" invalid dates. You would have to stage the data first to use it.

Sample:

Code Snippet

SELECT SalesOrderID, InvalidDate=OrderDate

FROM AdventureWorks.Sales.SalesOrderHeader sod

WHERE ISDATE(sod.OrderDate)=0

GO

Let us know if helpful.

SQLUSA  Sunday, September 28, 2008 7:29 PM
Hi

try this function

public static bool IsDate(string input)
{
try
{
DateTime.Parse(input);
return true;
}
catch
{
return false;
}
}


Catch the ouput outside and if it is not date set it to whatever you want.


Hope this helps and if it does please mark the post as answered. This gives credit to ppl who replied


Ashwani Roy - IMGROUP  Sunday, September 28, 2008 7:42 PM
Here is a small C# console app that wrote in VS C# 2005. Hope you can use the same code and mould it for script task in SSIS


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string dt ;
dt = "09/09/2008";
bool bl = IsDate(dt);

Console.WriteLine(dt+":Is date input");
Console.WriteLine(bl + ":Is date output");


dt = "09/100/2008";
bl = IsDate(dt);

Console.WriteLine(dt + ":Is date input");
Console.WriteLine(bl + ":Is date output");

}
public static bool IsDate(string input)
{
try
{
DateTime.Parse(input);
return true;
}
catch
{
return false;
}
}

}
}

Ashwani Roy - IMGROUP  Sunday, September 28, 2008 7:47 PM

You can use google to search for other answers

Custom Search

More Threads

• pda *.sdf database
• alfanumeric counters?
• Longarse txt Source files
• Data flow multiple sources to populate a destination
• Putting variables in SQL Strings
• vbscript ETL
• Using checkpoints in SQL Server Integration Services (SSIS) Packages
• Lost old DTS package password!
• How to configure Xml configuration in sql server agent Job
• Little Urgent.....Advice on Logic flow in SSIS transformation......!