如何使用Case语句更改数据类型

时间:2016-10-18 21:43:06

标签: oracle date select type-conversion

我有这样的代码

Sub Distinct()
Const TRNS_START As String = "TRNS"
    Const TRNS_END As String = "ENDTRNS"
    Const COMPANY As String = "Triumph Foods LLC"

    Dim searchRng As Range, copyRngStart As Range, copyRngEnd As Range

    Set searchRng = Worksheets("Information").Range("A1")

    ' Enter/continue loop while A-column is non-empty
    Do While searchRng.Value <> ""

        ' When we encounter the string TRNS in column A and Triumph Foods LLC in column E
        If searchRng.Value = TRNS_START And _
           searchRng.Offset(0, 4).Value = COMPANY Then

            Set copyRngStart = searchRng ' Set the start of the copy area
        End If

        ' When we encounter the string ENDTRNS
        '    (*and had a start cell already*)
        If searchRng.Value = TRNS_END And Not copyRngStart Is Nothing Then

            Set copyRngEnd = searchRng.Offset(-1, 8)

            copyRngEnd.Worksheet.Range(copyRngStart, copyRngEnd).Copy _
              Destination:=Sheets("Display").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)

            Set copyRngStart = Nothing 'clear the "start" range

        End If

        Set searchRng = searchRng.Offset(1, 0)
    Loop


End Sub

错误是

ORA-00932;不一致的数据类型:预期CHAR得到DATE

gift_club_end_date的数据类型为VARCHAR2。

我想改为DATE

例如,如果有行

20160802

然后显示

2016年8月2日

但有些行为空白且为00000000

所以我想在这些行上显示/更改为空白,如果为空或00000000

如何在不造成错误的情况下进行编码

非常感谢

1 个答案:

答案 0 :(得分:1)

您可能需要以下内容:

with gift_clubs(gift_club_end_date) as
(
    select '20160802' from dual union all
    select '00000000' from dual union all
    select null       from dual
)
select case
        when gift_club_end_date = '00000000'
            then to_date(null)
        when gift_club_end_date is null
            then to_date(null)
        when gift_club_end_date = ' '
            then to_date(null)
        else
            to_date(gift_club_end_date, 'yyyymmdd')
       end
from gift_clubs

这考虑了null和包含单个空格的字符串的情况;您可能只需要其中一个,因此请相应地编辑该语句。