user2119912
user2119912

Reputation: 101

SQL Server 2008 - convert varchar to datetime column

I have a table with a varchar column (dateandtime) with date and time in the format dd/mm/yyyy hh:mm:ss and I need to convert it to a datetime column.

I have a temp column dateandtimetemp in the table .

Thanks

Upvotes: 0

Views: 24746

Answers (4)

Abhishek B Patel
Abhishek B Patel

Reputation: 935

Try

SELECT CONVERT(VARCHAR(30),GETDATE(),113) ;

it return result in following format

15 May 2013 16:26:29:850

Upvotes: 2

heikkim
heikkim

Reputation: 2975

Try this

SELECT CONVERT(DATETIME, '15/05/2013 11:12:13', 103)

Upvotes: 0

marc_s
marc_s

Reputation: 754268

So then just go ahead and try it!

UPDATE dbo.YourTable
SET DateAndTimeTemp = CAST(DateAndTime AS DATETIME)

and see if it works. If your input data is really always properly defined - you should have no issues here.

This of course depends on what langauge/dateformat setting you have activated in your database - so that might be the first problem you encounter.

If you do have issues, then you can always "clean up" your input data and try again ...

Upvotes: 1

Semih Yagcioglu
Semih Yagcioglu

Reputation: 4101

Try this:

SELECT CONVERT(Datetime, '15/05/2013 13:55:12', 104)

It should return : 2013-05-15 13:55:12.000

Upvotes: 4

Related Questions