SQL / SSIS如何从YYYYDDD格式转换为常规DateTime

时间:2015-08-13 01:11:49

标签: sql date datetime ssis

快问你们。我正在编写一些ETL,我需要从平面文件中解析出一个字段,其中日期列为YYYYDDD格式。目标是将其加载到数据库列中并显示为YYYY-MM-DD。

如何在SSIS包中做得最好?有没有办法在包中做到这一点?记录体首先被加载到原始表中,所以如果您知道自定义SQL来处理这个问题,我可以将它添加到SQL步骤来解析它。

非常感谢任何帮助。谢谢,

2 个答案:

答案 0 :(得分:0)

这样的事情怎么样:

select dateadd(day, cast(right(YYYYDDD, 3) as int) - 1,
               cast(left(YYYYDD, 4) + '0101' as date))

SQL Server将YYYYMMDD格式的字符串识别为日期,并且应始终将它们转换为无错误。

答案 1 :(得分:0)

当我遇到YYYYDDD日期时,我会在脚本转换中使用VB将它们转换为实际日期:

Row.MaturityDate = DateSerial(CInt(Left(Row.MaturityDate,4)),1,CInt(Right(Row.MaturityDate,3))).ToShortDateString

在这种情况下, MaturityDate 列是文本/字符串列,并设置为ReadWrite。因此,日期从 MaturityDate 列中读取,修改后再写回 MaturityDate 列。

您可以添加一个新列(具有不同的数据类型,例如日期数据类型之一)到脚本转换,从传入列读取并写入新的传出列...保持传入列可以参考。

我遇到了许多不同格式的日期。我写了一个VB UDF处理我到目前为止遇到的格式。如果它对你有用,那么你非常欢迎它(注意:我比VB.Net更熟悉VB6 / VBA,所以这个UDF更多是VBA风格,但仍然适用于VB.Net ):

    Public Function ParseDate(ByVal pstrDate As String, ByVal pstrFormat As String, Optional ByVal pblnMEDate As Boolean = False) As Date
    Dim pstrYear As String
    Dim pstrMonth As String
    Dim pstrDay As String

    Dim pintYear As Integer
    Dim pintMonth As Integer
    Dim pintDay As Integer

    Dim pint4DigitCurrentYear As Integer
    Dim pint2DigitCurrentYear As Integer

    Dim pdteDate As Date

    Dim pintLen As Integer

    'pint4DigitCurrentYear = Year(mdteMEDate)'
    'pint2DigitCurrentYear = pint4DigitCurrentYear - 2000'


    pstrFormat = UCase(pstrFormat)

    Select Case pstrFormat
        Case "YYYY MMM"
            pstrYear = Left(pstrDate, 4)
            pstrMonth = Right(pstrDate, 3)
            pstrDate = pstrMonth & " " & pstrYear
            pdteDate = DateValue(pstrDate)


        Case "MMYYYY"
            pstrYear = Right(pstrDate, 4)
            pstrMonth = Left(pstrDate, 2)
            pstrDate = pstrMonth & " " & pstrYear
            pdteDate = DateValue(pstrDate)


        Case "MMMYY"
            pstrDate = Left(pstrDate, Len(pstrDate) - 2) & " 20" & Right(pstrDate, 2)
            pstrDate = Replace(pstrDate, "JLY", "JUL")
            pdteDate = DateValue(pstrDate)

        Case "MMM YY"
            pstrDate = Left(pstrDate, Len(pstrDate) - 2) & "20" & Right(pstrDate, 2)
            pstrDate = Replace(pstrDate, "Sept", "Sep")
            pdteDate = DateValue(pstrDate)

        Case "MMMYYYY", "MMM YYYY"
            pstrYear = Right(pstrDate, 4)
            pstrMonth = Left(pstrDate, 3)
            pstrDate = pstrMonth & " " & pstrYear
            pstrDate = Replace(pstrDate, "Sept", "Sep")
            pdteDate = DateValue(pstrDate)


        Case "MMMM YY"
            pstrYear = "20" & Right(pstrDate, 2)
            pstrMonth = Trim(Left(pstrDate, Len(pstrDate) - 2))
            pstrDate = pstrMonth & " " & pstrYear
            pdteDate = DateValue(pstrDate)

        Case "MMMMYYYY"
            pstrYear = Right(pstrDate, 4)
            pstrMonth = Left(pstrDate, Len(pstrDate) - 4)
            pstrDate = pstrMonth & " " & pstrYear
            pdteDate = DateValue(pstrDate)


        Case "DATEVALUE"
            pdteDate = DateValue(pstrDate)

        Case Else

            Select Case pstrFormat
                Case "YYYYDDD"
                    pstrYear = Left(pstrDate, 4)
                    pstrMonth = "1"
                    pstrDay = Right(pstrDate, 3)

                Case "YYYYMMDD"
                    pstrYear = Left(pstrDate, 4)
                    pstrMonth = Mid(pstrDate, 5, 2)
                    pstrDay = Right(pstrDate, 2)

                Case "YYYY-MM-DD"
                    pstrYear = Left(pstrDate, 4)
                    pstrMonth = Mid(pstrDate, 6, 2)
                    pstrDay = Right(pstrDate, 2)

                Case "YYMMDD"
                    pstrYear = "20" & Left(pstrDate, 2)
                    pstrMonth = Mid(pstrDate, 3, 2)
                    pstrDay = Right(pstrDate, 2)

                Case "YYYY-MM", "YYYYMM", "YYYY MM"
                    pstrYear = Left(pstrDate, 4)
                    pstrMonth = Right(pstrDate, 2)
                    pstrDay = "1"

                Case "YYMM"
                    pstrYear = "20" & Left(pstrDate, 2)
                    pstrMonth = Right(pstrDate, 2)
                    pstrDay = "1"



                Case "DDMMYYYY"
                    pstrYear = Right(pstrDate, 4)
                    pstrMonth = Mid(pstrDate, 3, 2)
                    pstrDay = Left(pstrDate, 2)



                Case "MDDYYYY", "MMDDYYYY"
                    pintLen = Len(pstrDate)
                    pstrYear = Right(pstrDate, 4)
                    pstrMonth = Left(pstrDate, pintLen - 6)
                    pstrDay = Mid(pstrDate, pintLen - 5, 2)
                Case "MDDYY"
                    pintLen = Len(pstrDate)
                    pstrYear = Right(pstrDate, 2)
                    pstrMonth = Left(pstrDate, pintLen - 4)
                    pstrDay = Mid(pstrDate, pintLen - 3, 2)
                Case "MMDDYY"
                    pstrYear = "20" & Right(pstrDate, 2)
                    pstrMonth = Left(pstrDate, 2)
                    pstrDay = Mid(pstrDate, 3, 2)

                Case "MM-DD-YYYY", "M-DD-YYYY", "M.DD.YYYY"
                    pstrYear = Right(pstrDate, 4)
                    pstrMonth = Left(pstrDate, Len(pstrDate) - 8)
                    pstrDay = Mid(pstrDate, Len(pstrDate) - 6, 2)

                Case "M-DD-YY", "MM-DD-YY", "M/DD/YY", "MM/DD/YY"
                    pstrYear = Right(pstrDate, 2)
                    pstrMonth = Left(pstrDate, Len(pstrDate) - 6)
                    pstrDay = Mid(pstrDate, Len(pstrDate) - 4, 2)

                Case "M-DD-YY PAST", "MM-DD-YY PAST", "M/DD/YY PAST", "MM/DD/YY PAST"
                    pintLen = Len(pstrDate)
                    pstrYear = Right(pstrDate, 2)
                    pintYear = CInt(pstrYear)

                    If pintYear > pint2DigitCurrentYear Then
                        pstrYear = "19" & pstrYear
                    ElseIf pintYear <= pint2DigitCurrentYear Then
                        pstrYear = "20" & pstrYear
                    Else
                        pstrYear = "99" & pstrYear
                    End If

                    pstrMonth = Left(pstrDate, Len(pstrDate) - 6)
                    pstrDay = Mid(pstrDate, Len(pstrDate) - 4, 2)

                Case "MMYYDD PAST"
                    'pintLen = Len(pstrDate)'
                    pstrYear = Mid(pstrDate, 3, 2)
                    pintYear = CInt(pstrYear)

                    If pintYear > pint2DigitCurrentYear Then
                        pstrYear = "19" & pstrYear
                    ElseIf pintYear <= pint2DigitCurrentYear Then
                        pstrYear = "20" & pstrYear
                    Else
                        pstrYear = "99" & pstrYear
                    End If

                    pstrMonth = Left(pstrDate, 2)
                    pstrDay = Right(pstrDate, 2)

                Case "DT6 PAST"
                    pintLen = Len(pstrDate)
                    pstrYear = Right(pstrDate, 2)
                    pintYear = CInt(pstrYear)

                    If pintYear > pint2DigitCurrentYear Then
                        pstrYear = "19" & pstrYear
                    ElseIf pintYear <= pint2DigitCurrentYear Then
                        pstrYear = "20" & pstrYear
                    Else
                        pstrYear = "99" & pstrYear
                    End If

                    pstrMonth = Left(pstrDate, pintLen - 4)
                    pstrDay = Mid(pstrDate, pintLen - 3, 2)
                Case "DT6 PAST PLUS 10"
                    pintLen = Len(pstrDate)
                    'pint4DigitCurrentYear = Year(mdteMEDate) + 10'
                    pint2DigitCurrentYear = pint4DigitCurrentYear - 2000
                    pstrYear = Right(pstrDate, 2)
                    pintYear = CInt(pstrYear)

                    If pintYear > pint2DigitCurrentYear Then
                        pstrYear = "19" & pstrYear
                    ElseIf pintYear <= pint2DigitCurrentYear Then
                        pstrYear = "20" & pstrYear
                    Else
                        pstrYear = "99" & pstrYear
                    End If

                    pstrMonth = Left(pstrDate, pintLen - 4)
                    pstrDay = Mid(pstrDate, pintLen - 3, 2)


                Case "MYY"
                    pstrYear = "20" & Right(pstrDate, 2)
                    pstrMonth = Left(pstrDate, Len(pstrDate) - 2)
                    pstrDay = "1"
                Case "MMYY", "MM-YY", "MM/YY"
                    pstrDate = Replace(pstrDate, "-", "")
                    pstrDate = Replace(pstrDate, "/", "")
                    pstrYear = "20" & Right(pstrDate, 2)
                    pstrMonth = Left(pstrDate, 2)
                    pstrDay = "1"
                Case "MMYY PAST", "MM-YY PAST", "MM/YY PAST"
                    pstrDate = Replace(pstrDate, "-", "")
                    pstrDate = Replace(pstrDate, "/", "")
                    pstrYear = Right(pstrDate, 2)
                    pintYear = CInt(pstrYear)
                    If pintYear > pint2DigitCurrentYear Then
                        pstrYear = "19" & pstrYear
                    ElseIf pintYear <= pint2DigitCurrentYear Then
                        pstrYear = "20" & pstrYear
                    Else
                        pstrYear = "99" & pstrYear
                    End If
                    pstrMonth = Left(pstrDate, 2)
                    pstrDay = "1"
                Case "MM-YYYY"
                    pstrYear = Right(pstrDate, 4)
                    pstrMonth = Left(pstrDate, 2)
                    pstrDay = "1"
                Case "M-YYYY"
                    pstrYear = Right(pstrDate, 4)
                    pstrMonth = Left(pstrDate, Len(pstrDate) - 5)
                    pstrDay = "1"




                Case Else
                    MsgBox("Date Format does not exist in function." & vbCrLf & vbCrLf & "Please add it.", vbInformation, "Date Format")
                    'ParseDate = CVErr(xlErrRef)'
                    Exit Function
                    'pstrYear = "9999"'
                    'pstrMonth = "99"'
                    'pstrDay = "99"'
            End Select

            pintYear = CInt(pstrYear)
            pintMonth = CInt(pstrMonth)
            pintDay = CInt(pstrDay)

            pdteDate = DateSerial(pintYear, pintMonth, pintDay)
    End Select

    If pblnMEDate Then
        pdteDate = DateSerial(Year(pdteDate), Month(pdteDate) + 1, 0)
    End If

    'ParseDate = pdteDate.ToShortDateString'
    ParseDate = pdteDate


End Function

如果有人对我的代码进行了改进,我很乐意看到它们。

HTH