Convert varchar to datetime using VB script

余生长醉 提交于 2020-01-03 04:38:06

问题


I need to convert a date field which is in varchar format (DDMMYYYY) to DateTime using .Net. I have tried a number of solutions but all in vain. My source for that field is a flat file and my destination is a table. I am using the script in SSIS package.

Below is the sample:

The column SampleDate is in the ddmmyyyy format. I am converting it to MM/dd/YYYY in order to convert it to datetime format.

Dim retString1 As String
Dim slash As Char = ChrW(&H2F)
retString1 = Row.SampleDate.Substring(2, 2) & "/"
Dim retString2 As String
retString2 = Row.SampleDate.Substring(0, 2) & "/"
Dim retString3 As String
retString3 = Row.SampleDate.Substring(4, 4)
Dim retString4 As String
retString4 = retString1 & retString2 & retString3

Dim fromDt As Date = Convert.ToDateTime(retString4)
fromDt = Row.SampleDate

P.S. : I am not looking for SQL Query for the conversion so before commenting, please read the whole question thoroughly. I am looking for a VB code and not a SQL code.


回答1:


Just flip fromdt = Row.SampleDate

to

Row.SampleDate = fromdt

And your script should work but see below to simplify some if you want.

HOW TO DO IT WITHOUT A SCRIPT --see below for script method

You can make this a lot simpler for yourself if you use a Derived Column transformation instead! The data conversion transformation doesn't work because it doesn't allow you to manipulate the value of the column, but rather only the data type. But in the derived column you can cut up the string and cast the result as a date.

So Source to Derived Column to Destination in your data flow.

In the derived column your add a new column and use the following as the expression (replacing DateString with the name of your column containing the date in ddMMyyyy format)

Then use this as the Expression:

(DT_DATE)(SUBSTRING(DateString,3,2) + "/" + (LEFT(DateString,2) + "/" + RIGHT(DateString,4)))

The (dt_date) can be switched for another date format if you wish and that type cast will automatically set the Data Type on the Derived Column. Then in your destination map the new derived column to the destination column instead of your original column from your source.

That's it your done, no scripts.

HOW TO DO IT VIA A SCRIPT

To address the route if you wanted to stay with a script and perhaps inject some additional logic rather than simple conversion you can do it through a transformation script component in a your data flow task. It will take place of the derived column transformation above but essentially will do the same thing only through a script. I am putting steps in here that I am guessing you already know some of in case someone else stumbles on the post.

  • Add the script component when you do choose "transformation" and connect it with you source
  • Open Script component and go to the "Inputs and Outputs" section and add an output to hold the new column and set the datatype. A new column is necessary because you are changing data types

  • go to "Input Columns" section and choose SampleDate column

  • go back to "Script" section and choose Visual Basic as your script language.
  • Click Edit Script to begin your coding.
  • Scroll down till you find the sub Input0_ProcessInputRow(ByVal Row... this is where you will put your code.

This code works:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

    If Not Row.SampleDate_IsNull Then

        Row.DerivedDate = DateTime.ParseExact(Row.SampleDate, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture)

    End If
End Sub

As @Shiva in comments suggested you could expand this and use TryParseExact or wrap it in a try catch block so that if parsing the date fails it would simply clear/remove the value from the record and the import will continue. I leave that up to you as handling invalid data is more a question of business requirements.




回答2:


To convert a string to a date in VBScript you either use CDate() which does the conversion for you but depends on the current regional settings or DateSerial():

>> s = "13041953"
>> d = DateSerial(CInt(Right(s,4)), CInt(Mid(s,3,2)), CInt(Left(s,2)))
>> WScript.Echo s, TypeName(d), d
>>
13041953 Date 13.04.1953 (german locale)


来源:https://stackoverflow.com/questions/38201351/convert-varchar-to-datetime-using-vb-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!