SQLUSA

Microsoft SQL Server 2005 Best Practices

 

How to convert from string to datetime?

 

Execute the following script in Management Studio Query Editor to convert from string format to datetime:

DECLARE

@DateTimeValue varchar(30),

@DateValue char(8),

@TimeValue char(6)

 

 

SELECT

@DateValue = '20081023',

@TimeValue = '211920'

 

 

SELECT @DateTimeValue =

convert(varchar, convert(datetime, @DateValue), 110)

+ ' ' + substring(@TimeValue, 1, 2)

+ ':' + substring(@TimeValue, 3, 2)

+ ':' + substring(@TimeValue, 5, 2)

 

 

SELECT

DateInput = @DateValue,

TimeInput = @TimeValue,

DateTimeOutput = @DateTimeValue,

DateTimeFormat = convert(datetime, @DateTimeValue)

 

The World Leader in SQL Server 2005 Training
 
SQLUSA.com Home Page