将 NVARCHAR(255) 转换为 DATE

我正在尝试使用 Excel 将一些旧的 SQL Server 数据传输到 SQL Server.Import/Export Data 应用程序似乎自动将大多数数据列设置为 NVARCHAR(255).我遇到的问题是,我的一列应该是 DATE 类型,但其中的所有数据看起来都像这样 18.08.2000 14:48:15.

I'm trying to transfer some old SQL Server data using Excel into SQL Server. It seems that Import/Export Data application automatically sets most data columns to NVARCHAR(255). Problem I have, is one of my columns is supposed to be a DATE type, but all data in it looks like this 18.08.2000 14:48:15.

因此,当我尝试使用此查询时:

So, when I try to use this query:

SELECT CONVERT(Date, DATE_TIME,  113)
FROM someTable

我收到此错误:

消息 9807,级别 16,状态 0,第 1 行
输入字符串不遵循样式113,要么改变输入字符串,要么使用不同的样式.

Msg 9807, Level 16, State 0, Line 1
The input character string does not follow style 113, either change the input character string or use a different style.

CAST 和CONVERT (Transact-SQL) 在我的情况下有效.

非常感谢任何建议或帮助.

Any advise or help is greatly appreciated.

已解决:

UPDATE myTable
SET columnName = CONVERT(NVARCHAR(255),CONVERT(SMALLDATETIME, columnName,105))
ALTER TABLE myTable
ALTER COLUMN columnName SMALLDATETIME

推荐答案

所以,如果它对任何人有用,这就是将数据类型 NVARCHAR 更改为 DATETIME 的确切代码:

So, if it will ever become useful to anyone, this is the exact code that changes datatype NVARCHAR to DATETIME:

UPDATE myTable
SET columnName = CONVERT(NVARCHAR(255),CONVERT(SMALLDATETIME, columnName,105))
ALTER TABLE myTable
ALTER COLUMN columnName SMALLDATETIME

相关文章