2015-09-15 55 views
-1

這裏是我的表SQL轉換爲日期和/或時間從字符串出錯

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] = '[email protected]' , 
    [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] = 'A[email protected]' , 
    [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 

爲什麼我跑我收到一個轉換錯誤

Msg 241,Level 16,State 1,Line 1
從字符串轉換日期和/或時間時轉換失敗。

插入進入'DATETIME'列,插入的日期時間都被正確格式化,任何人都可以擺脫這種情況嗎?

UPDATE

這個問題似乎只在我的VB.NET應用出現。在SQL Server Management Studio中運行語句時,它很好,當它在我的VB.NET中執行時,它會作爲從字符到日期時間的對話失敗。

有什麼建議嗎?

+0

['SET DATEFORMAT mdy'](https://msdn.microsoft.com/en-us/library/ms189491.aspx)和/或使用[** ISO-8601 **](https://開頭的TechNet .microsoft.com/en-us/library/ms190977%28v = sql.90%29.aspx)日期時間文字格式。 – lad2025

+0

你可以一個一個評論可疑的列。找到錯誤列後,再重新考慮。 –

回答

2

的問題是你試圖把這個詞NULLtime_opened領域。 (這是一個日期時間字段)

更改它從[time_opened] = 'NULL'[time_opened] = NULL

UPDATE [FreshSystems].[dbo].[RPost_Receipts] 
SET  [sender_name] = 'RPost eSignOff Service' , 
    [sender_address] = '[email protected]' , 
    [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] = '[email protected]' , 
    [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 
0

我認爲日期時間格式是YY-MM-DD HH:MM:SS,而不是2015年9月15日10點36分: 29 AM 在更新表格之前進行轉換。

相關問題