将时间字符串更改为Sql server DateTime数据类型

时间:2009-05-12 10:04:49

标签: sql-server datetime time

如何将这些Date Time字符串更改为sql server DateTime类型:

"Thu May 07 19:19:27" 
"Thu May 07 19:19:33" 
"Thu May 07 19:19:34" 
"Thu May 07 19:19:34"
"Thu May 07 19:19:35" 

2 个答案:

答案 0 :(得分:2)

这是一段TSQL,您可以在存储过程或函数中使用它来将字符串转换为SQL DateTime。

  DECLARE
     @Year char(4), /* the DateTime needs a year */
     @DateString varchar(20),
     @DateVariable DateTime;

  SET @Year = '2009';
  SET @DateString = 'Thu May 07 19:19:27';  /* any of the dates in your list */

  SET @DateVariable = CONVERT(DateTime, @Year 
                            + SUBSTRING(@DateString, 4, LEN(@DateString)));

  /*
      After the conversion, @DateVariable contains '2009-05-07 19:19:27.000'
  */

答案 1 :(得分:0)

您是否正在考虑使用SQL存储的函数/过程或某些外部程序将这些日期字符串转换为DateTime值?