Solved

Unable to Convert varchar (Date) to datetime

Posted on 2004-04-21
14
1,026 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:synergeticsoul
  • 5
  • 4
  • 4
  • +1
14 Comments
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10880986
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
 

Author Comment

by:synergeticsoul
ID: 10881309
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
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 10881401
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 10881405
Once you correct (or delete) the invalid columns, then you can use ALTER to change the datatype of the column.
0
 
LVL 5

Assisted Solution

by:chaniewskim
chaniewskim earned 250 total points
ID: 10881506
If all dates are OK, then try it in QueryAnalyzer:

SET DATEFORMAT mdy
GO
ALTER TABLE YourTable ALTER COLUMN YourColumn datetime
GO
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10881520
I meant:

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

Author Comment

by:synergeticsoul
ID: 10881542
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
 

Author Comment

by:synergeticsoul
ID: 10881570
Additionally, through my programming (coding in CFML), I have dates inserted into the fields as: mm/dd/yy.
0
 
LVL 5

Expert Comment

by:chaniewskim
ID: 10881575
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
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 10881594
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
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 10881627
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
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 10881642
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
 

Author Comment

by:synergeticsoul
ID: 10887488
Thank you all.  I am going to try your suggestions.
0
 

Author Comment

by:synergeticsoul
ID: 10901032
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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question