Old ladyHow Big Is Your Package?
Size up and convert your DTS package to SSIS. If you have the biggest DTS package, win a gift card and certificate of a world record package size.  
Converting a string to a time and date - Mike Davis
in

Pragmatic Works

Mike Davis

Converting a string to a time and date

We had a customer that had time saved as a string and needed it converted to a date time field. They wanted to combine it with another date field. Here is a simple script to convert in situaions like this in an SSIS transformaion script. By the way some of the time strings were null and some had bogus times in them. After writing it I should have added a check to make sure the time is between 0000 and 2400.

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        Dim NewDateTime As Date
        Dim HourStr As String
        Dim MinuteStr As String

        'validate time is not null
        If Not (Row.NATIME_IsNull) Then

            'Validate  time is a 4 digit number
            If IsNumeric(Row.NATIME) And Row.NATIME.ToString.Length = 4 Then

                ' break time into hours and minutes
                HourStr = Row.NATIME.Substring(0, 2)
                MinuteStr = Row.NATIME.Substring(2, 2)

            Else ' iff time is not a 4 digit number
                HourStr = "00"
                MinuteStr = "00"
            End If

        Else ' if time is null

            HourStr = "00"
            MinuteStr = "00"

        End If

        'combine date and time
        NewDateTime = CDate(CStr(DateSerial(Year(Row.Date), Month(Row.Date), DatePart("d", Row.Date))) + " " + _
           CStr(TimeSerial(CInt(HourStr), CInt(MinuteStr), 0)))

        'output row
        Row.NewDateTime = NewDateTime

    End Sub

Comments

No Comments

About Mike Davis

Mike Davis, is a developer, consultant, trainer, and mentor who is enthusiastic about developing robust application for SQL server. He has expertise in many areas of Business Intelligence including Integration Services, Reporting Services, Database Administration, and .Net Software Development. Mike has created BI and software solutions for financial institutions and pragmatic works. He has developed .Net applications for SQL add-ons as well as standalone applications. Mike also participates as a speaker at events like SQL Server 2008 launch and SQL server user group meetings .
Copyright Pragmatic Works
Powered by Community Server (Non-Commercial Edition), by Telligent Systems