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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Convert varchar to datetime

Author  Topic 

jpiscit1
Posting Yak Master

130 Posts

Posted - 2002-11-14 : 14:49:23
Can any one show me an example of how one would convert data in a column that is varchar to datetime, if its even possible?

Thanks,

John P.


JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-11-14 : 15:26:57
cast('11/14/2002' as datetime)
or
cast(mycolumn as datetime)

you can also use convert(). See BOL for more examples.

hth,
Justin

Have you hugged your SQL Server today?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2002-11-14 : 15:47:18
Just make sure that your data in the varchar column is really a valid date and time.

Here is an example of convert:

SELECT CONVERT(datetime, myColumn)
FROM myTable

Justin shows how to do the cast way. For some reason, I always use CONVERT. I think it is a habit. Either way works though.

Go to Top of Page

royv
Constraint Violating Yak Guru

455 Posts

Posted - 2002-11-14 : 15:52:59
One advantage I can think of for using CONVERT is in the case that I'm in, which is porting code to another database, CONVERT is the more widely used one.

***************************************
Death must absolutely come to enemies of the code!
Go to Top of Page

baldeep
Starting Member

18 Posts

Posted - 2002-11-15 : 02:09:59
One other comment:

CONVERT(DATETIME, '19/08/2002', 103)

is valid as well. Kinda handy. Use of the style number is documented for DATETIME -> VARCHAR but BOL doesn't explicitly say that it works for VARCHAR -> DATETIME.

--Baldeep

Go to Top of Page
   

- Advertisement -