Modified Date Time String to Sql server DateTime Data Type

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

"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" 

      

0


a source to share


2 answers


Here is a TSQL snippet that you can use in a stored procedure or function to convert strings to 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


a source


Are you considering converting these date strings to DateTime values ​​using a SQL stored function / procedure, or in some external program?



0


a source







All Articles