Unable to Convert varchar (Date) to datetime

Hi,

I tried to manually change a few fields in my table that originally had datatypes of "varchar" to "datetime".  I am hoping that by converting to datetime, then I can display specific records in a range according to dates. However, when I went directly to my table and changed the value type of the field, authorization was denied. I got the following error messages:

1) Data may be lost converting
2) Then when I click okay, I get the following:

3) 'BBDirectory_BPD (dbname)' table
- Unable to modify table.  
ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting datetime from character string.

  How can I successfully convert the datatype from varchar to datetime?


Thanks in advance.
synergeticsoulAsked:
Who is Participating?
 
Scott PletcherSenior DBACommented:
Hmm, that conversion should work OK IF all dates are correct, so they must not be :-).  Try this to see if you have any bad columns:


SELECT *
FROM tableName
WHERE ISDATE(columnName) <> 1
0
 
chaniewskimCommented:
It looks like your varchar-dates are not compatible with your database date format.
Try to issue UPDATE command on your table converting strings to appropriate format. I can't help you further because I don't know what format use your current column and what is used for datetime in DB...
0
 
synergeticsoulAuthor Commented:
Although varchar was being used, dates were entered as follows: mm/dd/yy for all date-related fields.  

how would I correctly use the update command?  

Thanks.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Scott PletcherSenior DBACommented:
Once you correct (or delete) the invalid columns, then you can use ALTER to change the datatype of the column.
0
 
chaniewskimCommented:
If all dates are OK, then try it in QueryAnalyzer:

SET DATEFORMAT mdy
GO
ALTER TABLE YourTable ALTER COLUMN YourColumn datetime
GO
0
 
chaniewskimCommented:
I meant:

If all dates are OK, and you still cannot convert that column, then...
0
 
synergeticsoulAuthor Commented:
ScottPletcher,

I think that is the problem, I can not alter the datatype of the column.

For example, one of the 4 columns I need to convert is STARTDATE.  The datatype is currently varchar, 100.  I want to convert the datatype to datetime so that I can display the dates in a range through my query.  

How can I change the datatype without producing errors?  There are currently over 800 records and my head is on a plate.

Thanks.
0
 
synergeticsoulAuthor Commented:
Additionally, through my programming (coding in CFML), I have dates inserted into the fields as: mm/dd/yy.
0
 
chaniewskimCommented:
As Scott has written,

SELECT *
FROM tableName
WHERE ISDATE(columnName) <> 1

will return all rows which make problems with conversion. Then you probably have to correct them manually...
0
 
ChrisFretwellCommented:
it means that a) your server may be expecting a character dates to be in the format dd/mm/yy or b) one of your dates made in it wrong

If its a - then there are a few ways around. To find out easily, there are many ways but this quick query will do it

select convert(datetime, '12/31/99')

if it produces an error, then your server is expecting dd/mm/yy and you can parse out your input to change it.

If you get no errors from above, then you have 1 (or more) value(s) in your table that is wrong.
Finding it can be a pain. One way I've done this in the past (there are other ways) is
select identityfield, originalfield, convert(datetime,originalfield)
from table
order by identityfield

It will run, and produce results until it finds one record that is wrong. And you will know the last valid identityfield.
Then you can
select top 1 identityfield, originalfield
from table
where identityfield > [lastvalididentity]
order by identityfield

Then determine what the correct date should be, update the record and repeat the process.

This works if you have an identity field if you dont, then order by and select by the date field.
0
 
Scott PletcherSenior DBACommented:
Yes, chaniewskim is right, most likely you will need to correct them "manually", which might include an UPDATE statement(s) if certain conditions occurred often, that is, the same mistake was made multiple times.  

For example, let's say that several times the '/'s are omitted, so some data looks like this:

011304   (mmddyy = 01/13/04)
022704


Then you could do this:

UPDATE yourTable
SET dateColumn = LEFT(dateColumn, 2) + '/' + SUBSTRING(dateColumn, 3, 2) + '/' + SUBSTRING(dateColumn, 5, 2)
WHERE CHARINDEX('/', dateColumn) = 0
--AND ...any other needed qualifier(s)...


Note that of course the qualifiers are very important -- otherwise you'll damage existing dates that are already correct.
0
 
Scott PletcherSenior DBACommented:
Chris:

ISDATE(), unlike say ISNUMERIC(), is pretty reliable (at least I've never had a problem with it).  It can be used to find all invalid dates at one time.
0
 
synergeticsoulAuthor Commented:
Thank you all.  I am going to try your suggestions.
0
 
synergeticsoulAuthor Commented:
I was able to successfully pull the records using

SELECT *
FROM tableName
WHERE ISDATE(columnName) <> 1

There were only a few that had the incorrect formatting.  Thank You!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.