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
 varchar field that won't convert

Author  Topic 

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2009-03-25 : 10:17:42
Hi

I am having problems converting my one varchar field to a datetime field. I never had problems before. This is the formula that I was using...CONVERT(VARCHAR(10), [clm_epis], 112) AS [Send to PPO Date], but the numbers are still 20090324. what else can I do because there are alot of different dates.

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-25 : 10:20:51
Using 112 will convert your datetime field to yyyymmdd format which it did.

What do you want to convert it to?

Go to Top of Page

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2009-03-25 : 10:23:33
I wanted to to say 03/24/2009 not 20090324.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-25 : 10:30:11
Ok..Now I read you post again..and I see you say "problems converting my one varchar field to a datetime field"...

you can't use the CONVERT options on VARCHAR fields...it has to be a datetime field. If you want to convert a varchar field with a value '03/24/2009' to 20090324..you will have to use normal string operations.
Go to Top of Page

werhardt
Constraint Violating Yak Guru

270 Posts

Posted - 2009-03-25 : 10:34:09
okay thanks!
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-03-25 : 10:54:20
You could do this

declare @strDate varchar (10)
set @strDate = '20090324'


select convert(varchar(10),convert(datetime,@strDate),101)

Jim
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-25 : 12:55:27
quote:
Originally posted by werhardt

Hi

I am having problems converting my one varchar field to a datetime field. I never had problems before. This is the formula that I was using...CONVERT(VARCHAR(10), [clm_epis], 112) AS [Send to PPO Date], but the numbers are still 20090324. what else can I do because there are alot of different dates.


two questions

1. why are you using varchar to store dates? you should always try to use proper datatype for your variables. storing date values in varchar field will make date manipulations difficult

2. why are you trying to do formatting in t-sql? formatting is better dealt with at your front end where you have lots of formatting functions available. so if at all possible try to do the formatting at your front end.
Go to Top of Page
   

- Advertisement -