【问题标题】:SQL Conversion to Date And/Or Time From Character String Error从字符串错误到日期和/或时间的 SQL 转换
【发布时间】:2015-09-15 14:37:20
【问题描述】:

这是我的桌子

CREATE TABLE [dbo].[RPost_Receipts]
(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [branch] [int] NULL,
    [policyref] [varchar](10) NULL,
    [receipt_email_received] [datetime] NULL,
    [receipt_email_to_openattach] [int] NULL,
    [receipt_email_to_openattach_dt] [datetime] NULL,
    [sender_name] [varchar](50) NULL,
    [sender_address] [varchar](100) NULL,
    [subject] [varchar](160) NULL,
    [message_id] [varchar](100) NULL,
    [time_received] [datetime] NULL,
    [delivery_to] [varchar](100) NULL,
    [delivery_status] [varchar](100) NULL,
    [delivery_report] [varchar](max) NULL,
    [delivery_time_utc] [datetime] NULL,
    [delivery_time_local] [datetime] NULL,
    [time_opened] [datetime] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

这是我试图在同一张表上运行的以下UPDATE 查询

UPDATE [FreshSystems].[dbo].[RPost_Receipts]
SET [sender_name] = 'RPost eSignOff Service' ,
    [sender_address] = 'contracts@usw.rpost.net' ,
    [subject] = 'Re: REF: 02-OGKX02PC01 Your Insurance Policy (2 of 2)' ,
    [message_id] = '49B918875098C1EFCB5A33FDB2D446FF5C294ACE' ,
    [time_received] = '9/15/2015 10:36:29 AM' ,
    [delivery_to] = 'AutoSaintCS@Fresh.co.uk' ,
    [delivery_status] = 'Delivered to Mailserver' ,
    [delivery_report] = '250 OK id=1ZbnbS-0003fY-4y engine03-30179-2.icritical.com (192.162.216.4)' ,
    [delivery_time_utc] = '9/15/2015 10:36:47 AM' ,
    [delivery_time_local] = '9/15/2015 10:36:47 AM' ,
    [time_opened] = 'NULL'
WHERE [branch] = 02
  AND [policyref] = 'OGKX02PC01'
  AND [delivery_to] IS NULL

为什么我会收到转换错误

消息 241,第 16 级,状态 1,第 1 行
从字符串转换日期和/或时间时转换失败。

插入内容将进入“DATETIME”列,并且插入的日期时间格式均正确无误,谁能帮我解释一下?

更新

这个问题似乎只发生在我的 VB.NET 应用程序中。在 SQL Server Management Studio 中运行该语句时很好,当它在我的 VB.NET 中执行时,它作为从字符到日期时间的对话失败。

有什么建议吗?

【问题讨论】:

  • SET DATEFORMAT mdy 和/或对日期时间文字使用 ISO-8601 格式。
  • 您可以对可疑列一一进行评论。找到错误列后再重新思考。

标签: sql sql-server vb.net tsql


【解决方案1】:

问题是您试图将单词 NULL 放入 time_opened 字段。 (这是一个日期时间字段)

将其从 [time_opened] = 'NULL' 更改为 [time_opened] = NULL

UPDATE  [FreshSystems].[dbo].[RPost_Receipts]
SET     [sender_name] = 'RPost eSignOff Service' ,
    [sender_address] = 'contracts@usw.rpost.net' ,
    [subject] = 'Re: REF: 02-OGKX02PC01 Your Insurance Policy (2 of 2)' ,
    [message_id] = '49B918875098C1EFCB5A33FDB2D446FF5C294ACE' ,
    [time_received] = '9/15/2015 10:36:29 AM' ,
    [delivery_to] = 'AutoSaintCS@Fresh.co.uk' ,
    [delivery_status] = 'Delivered to Mailserver' ,
    [delivery_report] = '250 OK id=1ZbnbS-0003fY-4y engine03-30179-2.icritical.com (192.162.216.4)' ,
    [delivery_time_utc] = '9/15/2015 10:36:47 AM' ,
    [delivery_time_local] = '9/15/2015 10:36:47 AM' ,
    [time_opened] = NULL
    WHERE   [branch] = 02
    AND [policyref] = 'OGKX02PC01'
    AND [delivery_to] IS NULL

【讨论】:

    【解决方案2】:

    我认为日期时间格式是 yy-mm-dd hh:mm:ss 而不是 9/15/2015 10:36:29 AM 在更新表格之前转换它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多