在sql server

时间:2015-06-25 11:32:38

标签: sql-server sql-server-2008

我有一张表中有nvarchar个数据。

Table
18-FEB-11 10.29.52.000000000 AM
20-FEB-11 04.10.40.000000000 PM
23-SEP-10 10.34.57.714000000 AM
08-OCT-10 09.41.16.921000000 PM

我想将此字段转换为sql server 2008 R2中的小日期时间

我如何转换它。

Expected OUTPUT:

2011-02-18 10:29:52
2011-02-20 16:10:40
2010-09-23 10:34:57
2010-10-08 21:41:16

2 个答案:

答案 0 :(得分:3)

请试试这个

declare @s_date as nvarchar(100)= '18-FEB-11 10.29.52.000000000 AM'
SELECT CONVERT(nvarchar(100),CAST(REPLACE(REPLACE(@s_date, SUBSTRING(@s_date, 19, 10),''),'.',':') as datetime), 120)

result : 2011-02-18 10:29:52

答案 1 :(得分:1)

这也应该有效:

DECLARE @datestring NVARCHAR(50) = '08-OCT-10 09.41.16.921000000 PM'
SELECT CAST(REPLACE(LEFT(@datestring,18),'.',':') + RIGHT(@datestring,2) AS SMALLDATETIME)