我想將在SQL Server 2008數據庫中以字符串形式存儲的日期轉換爲smalldatetime
。在SQL Server 2008中將字符串轉換爲日期格式
所保存的字符串格式爲16/12/2007
,我想刪除/,取而代之的是 - 得到正確的日期格式,它是16-12-2007
我收到以下錯誤
Conversion from string "16/12/2007" to type 'Date' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Conversion from string "16/12/2007" to type 'Date' is not valid.
Source Error:
Line 34: NewsItem.Visible = True
Line 35: NewsItem.Date_Modified = CDate(GetContent.Ndate)
Line 36: NewsItem.Date_Published = CDate(GetContent.Ndate)
我想到的創建一個函數,用-
替換/
字符,然後更新數據庫,但這需要很長時間。
如果您可以避免將日期作爲字符串存儲在首位,那麼問題就不會出現。 SQL Server具有完全可用的'date'和'datetime2'數據類型,ADO.NET知道如何翻譯.NET .NET DateTime類型。如果您偏離此目的並使用不合適的存儲類型,則只會遇到格式問題。 –