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

时间:2022-06-30 16:32:32

how to change these Date Time strings to sql server DateTime Type:

如何将这些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 个解决方案

#1


Here's a snippet of TSQL that you may be able to use in a stored procedure or function to convert the strings into SQL DateTime.

这是一段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'
  */

#2


Are you looking at converting those date strings into DateTime values using an SQL stored function/procedure or in some external program?

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

#1


Here's a snippet of TSQL that you may be able to use in a stored procedure or function to convert the strings into SQL DateTime.

这是一段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'
  */

#2


Are you looking at converting those date strings into DateTime values using an SQL stored function/procedure or in some external program?

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