我想将在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类型。如果您偏离此目的并使用不合适的存储类型,则只会遇到格式问题。 –