Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Convert Varchar to Datetime

Author  Topic 

dr223
Constraint Violating Yak Guru

444 Posts

Posted - 2014-06-19 : 10:38:25
Hi,

I have a column on my table with the varchar(50) datatype. The whole column has the value 2014-04-31

I want to Convert the varchar(50)datatype to datetime datatype.

The table name is called - dbo.pracs
the column name is - LastDatUpaded

Thanks in advance..

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-06-19 : 11:07:14
That's an invalid date! (30 days hath September, April....)

However, conversion for valid dates is easy:


select cast('2014-04-30' as datetime) as mydatetime
Go to Top of Page

dr223
Constraint Violating Yak Guru

444 Posts

Posted - 2014-06-19 : 11:30:10

I want to update the dbo.pracs - column LastDatUpdated with the result.

Than I asssume I can easily change the Varchar dataype to datetime

Please help how I can update the table..

Thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-06-19 : 11:33:18
You can't convert an invalid date value to a date. So, how do you want to handle invalid date values?
Go to Top of Page

dr223
Constraint Violating Yak Guru

444 Posts

Posted - 2014-06-19 : 11:36:29


Sorry - It's 2014-04-30
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-06-19 : 11:42:39
I understand that you want to:

1. Alter the table to change the column LastDatUpaded (is that the real name? seems rather odd!) from varchar(50) to datetime. Is that correct?

IF so, this should do the trick:


alter table dbo.pracs
alter column LastDatUpaded datetime
Go to Top of Page

dr223
Constraint Violating Yak Guru

444 Posts

Posted - 2014-06-19 : 11:44:13
Fantastic... Thank you
Go to Top of Page
   

- Advertisement -