Solved

Error converting data type DBTYPE_DBTIMESTAMP to datetime.

Posted on 2008-10-02
10
1,333 Views
Last Modified: 2012-05-05
I was working in a test enviroment on a Windows 2003 Server with SQL Server 2005 and the views I created run fine.  Now I have moved over to our production enviroment and I am receiving an error.
Error converting data type DBTYPE_DBTIMESTAMP to datetime.

There are no bad dates in the table however there is a date of 1/15/1905.  I cannot figure out what is the bad date.  Is there a setting in SQL I can change?  I am running queries against a linked server.
0
Comment
Question by:vzorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 

Author Comment

by:vzorn
ID: 22631483
I found the bad date, it is 1/15/1905.  This should be a valid date.  What can I do in SQL to change this?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22636980
Possibly your regional settings are attempting to interpret it as dd/mm/yyyy.

Is it stored in a datetime field or a varchar field?
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22637065
>>I am running queries against a linked server.

Is the linked server also a SQL Server 2005?

And is the date field really a DateTime?  It's not a TIMESTAMP, is it?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:vzorn
ID: 22637266
The linked server is a Progress database and the field is a varchar.  ;It is a user input field.
The strange thing is it worked in our test enviroment with the same set of data and now this server is erroring.   It is definately stopping at this record with the 1/15/1905.
My query is just a "Select [visit-start-date], [visit-id], [client-id] from LINKEDSRV..PUB.[pt-visit]"
No date parameters.

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22637331
If it's user input data, good luck.

Converting dd/mm/yy from varchar to datetime is:

select convert(datetime, yourfield,103)

mm/dd/yy:
select convert(datetime, yourfield,101)

There's no way to handle this in code reliable.

What if the user enters '02/06/08'?  Is that Feb 6 or June 2?




0
 

Author Comment

by:vzorn
ID: 22639522
The software that is using the Progress database controls the date input as mm/dd/yy.
I will try the convert function.
But why is MS SQL not recognizing 1/15/1905 as a valid date?
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22639552
Regional settings perhaps.  Any time you are converting a string to a datetime in SQL server, you are best explicitly setting the format using one of the format codes.  Like I put in http:#22637331.  It will ensure that SQL server is doing it right.  If you data is in mm/dd/yyyy format, instead of just doing:

convert(datetime, yourfield)

do:

convert(datetime,yourfield,101)

If you still get an error, then you have some data that is probably dd/mm/yyyy.
0
 

Author Comment

by:vzorn
ID: 22645546
It is almost working.  The query below works fine, but I need to pass a parameter.  I get the following error when I pass a parameter.

"The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value."

SELECT     pt_visit.[client-id], CAST(pt_visit.[discipline-code] AS nvarchar(50)) AS DisciplineCode, CONVERT(DATETIME,pt_visit.VisitStartDateChar) AS VisitStartDate, 
                      ts_entry.[sub-type-id], ts_entry.[visit-id]
FROM         dbo.view_pt_visit AS pt_visit INNER JOIN
                      dbo.view_ts_entry AS ts_entry ON pt_visit.[visit-id] = ts_entry.[visit-id]
WHERE     (ts_entry.[type-id] = 3) AND (isDate(pt_visit.VisitStartDateChar) = 1) AND (CAST(pt_visit.VisitStartDateChar AS DATETIME) > '1/1/1900'))
 
 
BUT THIS GIVES ME THE ERROR:
SELECT * FROM (
SELECT     pt_visit.[client-id], CAST(pt_visit.[discipline-code] AS nvarchar(50)) AS DisciplineCode, CONVERT(DATETIME,pt_visit.VisitStartDateChar) AS VisitStartDate, 
                      ts_entry.[sub-type-id], ts_entry.[visit-id]
FROM         dbo.view_pt_visit AS pt_visit INNER JOIN
                      dbo.view_ts_entry AS ts_entry ON pt_visit.[visit-id] = ts_entry.[visit-id]
WHERE     (ts_entry.[type-id] = 3) AND (isDate(pt_visit.VisitStartDateChar) = 1) AND (CAST(pt_visit.VisitStartDateChar AS DATETIME) > '1/1/1900'))
AS C WHERE CAST(C.VisitStartDate as DateTime) >='1/1/2008'
 
I HAVE TRIED:
 
WHERE CAST(C.VisitStartDate as DateTime) >='1/1/2008'
WHERE CAST(C.VisitStartDate as DateTime) >='2008-01-01'
WHERE CONVERT(DATETIME,C.VisitStartDate,103) >='1/1/2008'

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22647110
You would be best served to do a cast of your date.  Like I mentioned earlier, your regional settings are probably doing it.

Wrap your date in this.

select convert(datetime, '1/1/2008', 101) -- mm/dd/yyyy
or
select convert(datetime, '1/1/2008', 103) -- dd/mm/yyyy

0
 

Author Comment

by:vzorn
ID: 22704774
Turns out if I put an isDate() in the WHERE clause before the CONVERT(DATETIME,value) it works.

Thanks for your help.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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