年、月、日参数描述了无法表示的 DateTime 如何忽略

发布于 2024-11-08 06:23:42 字数 508 浏览 1 评论 0原文

我使用 ODBC 从普遍使用的 PSQL 数据库读取数据,在某些情况下,日期列可以包含 00/00/0000 日期。我并不真正关心无效日期,所以有什么方法可以将所有这些不可表示的日期转换为 Null 或某个特定日期,而不是查询失败。

编辑:

以下显示了我正在使用的代码以及失败的地方:

Private _connODBC As OdbcConnection

Dim dt As New DataTable
_connODBC = New OdbcConnection(txtConnectionString.Text)
_connODBC.Open()

Dim dataadapter = New OdbcDataAdapter(QueryString, _connODBC)
dataadapter.Fill(dt) '<--- This line throws the unrepresentable date time error
_connODBC.Close()

I am using ODBC to read data from a pervasive PSQL database and in some scenarios, the date column can contain 00/00/0000 date. I don't really care about invalid dates so is there some way I can convert all of these un-representable dates into Null or some specific date instead of the query failing.

EDIT:

The following shows the code I am using and where it is failing:

Private _connODBC As OdbcConnection

Dim dt As New DataTable
_connODBC = New OdbcConnection(txtConnectionString.Text)
_connODBC.Open()

Dim dataadapter = New OdbcDataAdapter(QueryString, _connODBC)
dataadapter.Fill(dt) '<--- This line throws the unrepresentable date time error
_connODBC.Close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离线来电— 2024-11-15 06:23:49

间接地,如果您将其导入文本字段而不是日期,那么在后面的步骤中,您可以根据需要识别和修改文本值。

Indirectly, if you import it into a text field instead of date, then, in a later step, you can identify and modify the text value however you want.

夜雨飘雪 2024-11-15 06:23:47

@SSS 让我想到这可以在查询中完成,尽管它有点冗长:

SELECT CASE CONVERT([updated date], SQL_CHAR) 
WHEN '0000-00-00' THEN NULL 
ELSE [updated date] END 
AS [updated date] FROM fingerscans

虽然我希望解决方案不依赖于我在读取数据库时必须记住执行此操作!

@SSS got me me thinking that this could be done in the query, although it is a bit long winded:

SELECT CASE CONVERT([updated date], SQL_CHAR) 
WHEN '0000-00-00' THEN NULL 
ELSE [updated date] END 
AS [updated date] FROM fingerscans

Although I would like a solution doesn't rely on me having to remember to do this whenever I read the database!

提笔书几行 2024-11-15 06:23:46

您可以使用 可空 DateTime - 如果该值无效,请将其设置为null (Nothing),否则为日期。

MyDate as Nullable(Of DateTime)

You can use a nullable DateTime - if the value is invalid set it to null (Nothing), otherwise to the date.

MyDate as Nullable(Of DateTime)
翻身的咸鱼 2024-11-15 06:23:45

我也遇到过同样的问题。不久前,我忘记了确切的细节,但问题是 Pervasive.SQL 使用 &H0(即零)来表示缺失的日期,但 &H0 对于 ODBC 中的日期字段无效。

我的解决方案是将字段转换为 SQL 中的字符串,因此在 QueryString 中使用它: CONVERT(datefield,SQL_CHAR)

然后编写以下函数将其转换回日期(&H0 成为新日期):

 ''' <summary>
''' Enables a Btrieve Date column that contains 0x0 values to be accessed. Use 'SELECT CONVERT(datefield, SQL_CHAR)' to access the field, then this function to convert the result back to a date. A New Date is returned if the record has a 0x0 date
''' </summary>
''' <param name="Expression"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function SQL_CHAR2Date(ByVal Expression As String) As Date
    'check Expression is in the correct format
    Dim strMap As String = ""
    For i As Integer = 0 To Expression.Length - 1
        Select Case Expression.Substring(i, 1)
            Case "0" To "9" : strMap &= "0"
            Case "-" : strMap &= "-"
            Case Else : strMap &= Expression.Substring(i, 1)
        End Select
    Next i
    Select Case strMap
        Case "0000-00-00"
        Case Else
            Throw New ApplicationException("SQL_CHAR2Date: invalid input parameter")
    End Select

    Dim y As Integer = CInt(Expression.Substring(0, 4))
    Dim m As Integer = CInt(Expression.Substring(5, 2))
    Dim d As Integer = CInt(Expression.Substring(8, 2))

    If y = 0 And m = 0 And d = 0 Then
        Return New Date
    Else
        Return DateSerial(y, m, d)
    End If
End Function

I've had this exact same problem. It was a while ago and I forget the exact details, but the problem is that the Pervasive.SQL uses &H0 (ie. zero) to represent a missing date, but &H0 is invalid for a date field in ODBC.

My solution was to cast the field to a string in the SQL, so use this in your QueryString: CONVERT(datefield,SQL_CHAR)

I then wrote the following function to convert it back to a date (&H0 becomes a New Date):

 ''' <summary>
''' Enables a Btrieve Date column that contains 0x0 values to be accessed. Use 'SELECT CONVERT(datefield, SQL_CHAR)' to access the field, then this function to convert the result back to a date. A New Date is returned if the record has a 0x0 date
''' </summary>
''' <param name="Expression"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function SQL_CHAR2Date(ByVal Expression As String) As Date
    'check Expression is in the correct format
    Dim strMap As String = ""
    For i As Integer = 0 To Expression.Length - 1
        Select Case Expression.Substring(i, 1)
            Case "0" To "9" : strMap &= "0"
            Case "-" : strMap &= "-"
            Case Else : strMap &= Expression.Substring(i, 1)
        End Select
    Next i
    Select Case strMap
        Case "0000-00-00"
        Case Else
            Throw New ApplicationException("SQL_CHAR2Date: invalid input parameter")
    End Select

    Dim y As Integer = CInt(Expression.Substring(0, 4))
    Dim m As Integer = CInt(Expression.Substring(5, 2))
    Dim d As Integer = CInt(Expression.Substring(8, 2))

    If y = 0 And m = 0 And d = 0 Then
        Return New Date
    Else
        Return DateSerial(y, m, d)
    End If
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文