Solved

Microsoft SQL not converting text to DateTime before adding values.

Posted on 2011-02-26
14
1,271 Views
Last Modified: 2013-12-11

I'm getting a type conversion error when calling a Microsoft Dynamics GP SQL Stored Procedure because it doesn't convert data type before adding.


The error is coming from a call like this inside the SQL stored procedure:

       @date = CONVERT(char(10),@MinDate,102) + CONVERT(varchar(8),GETDATE(),108)

Where
       CONVERT(char(10),@MinDate,102)     =     ‘1900.01.01’
       CONVERT(varchar(8),GETDATE(),108)  =     ‘04:41:52’  

It’s concatinating the two text strings before converting to the DateTime data type like this:

       @date = (‘1900.01.01’ + ‘04:41:52’)

You can get the proper result by changing processing order as follows:

       ((@date = ‘1900.01.01’) + ‘04:41:52’)

I’m assuming there must be a setting in SQL as there’s no way this call would be working in Dynamics GP.

Any help would be appreciated. The actual query and error are:

================

SET DATEFORMAT YMD
declare @enttme datetime = NULL
declare @MinDate datetime

/* sets the MinDate to the base date of 1900-01-01 */
exec smGetMinDate @MinDate output

if @enttme is NULL   begin
      select @enttme = CONVERT(char(10),@MinDate,102) + CONVERT(char(5),GETDATE(),108)  
end

      Msg 241, Level 16, State 1, Line 7
      Conversion failed when converting date and/or time from character string.      

0
Comment
Question by:papuleo
  • 4
  • 2
  • 2
  • +3
14 Comments
 
LVL 11

Expert Comment

by:dougaug
ID: 34987009
Try to add a space between date and time strings before convert to a datetime.

      select @enttme = CONVERT(char(10),@MinDate,102) + ' ' +CONVERT(char(5),GETDATE(),108)
0
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 34987012
I think you need to add space

select @enttme = CONVERT(char(10),@MinDate,102) + ' ' + CONVERT(char(5),GETDATE(),108)

Open in new window

0
 
LVL 1

Author Comment

by:papuleo
ID: 34987061
Unfortunately - this is a GP Dynamics stored procedure and can't be modified.
0
 
LVL 11

Expert Comment

by:dougaug
ID: 34990325
The code you have posted above (SET DATEFORMAT YTD ....) is inside your application or GP Dynamics stored procedure?
0
 
LVL 1

Author Comment

by:papuleo
ID: 34991175
The SQL code in my post iis directly from the GP Dynamics stored procedure.
0
 
LVL 18

Expert Comment

by:Victoria Yudin
ID: 34991293
What are you short date settings (under regional settings) on your SQL server and client computer (if you are getting this error while running the GP application on a client)?  If they are not MM/dd/yyyy can you try switching to that and see if it makes a difference?  

If that does not help, when does this error come up?  What process inside GP are you running?  Can you post the complete and exact wording of the error message, as well?
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 11

Expert Comment

by:JoeNuvo
ID: 34991323
I'm not really sure about Dynamic GP
but this part sure is a problem

select @enttme = CONVERT(char(10),@MinDate,102) + CONVERT(char(5),GETDATE(),108)

in order to stop the error you mention, it must be modify to be either
select @enttme = CONVERT(char(11),@MinDate,102) + CONVERT(char(5),GETDATE(),108)
or
select @enttme = CONVERT(char(10),@MinDate,102) + ' ' + CONVERT(char(5),GETDATE(),108)
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 34992230
What this means is that you are the only Dynamics user who has used this particular feature, or at least the only one who published this problem. If you don't want or can't fix this stored procedure, then your only recourse if opening support incident with Microsoft. This will cost $99, but once they confirm it, they will refund them. I have dozens of such incidents and not once paid $1 in the end. Perhaps this particular piece of code was created by offshored programmers in a country where this conversion does result in the space in the middle.
0
 
LVL 10

Expert Comment

by:Abdulmalek_Hamsho
ID: 34992577
But there's no problem in the smGetMinDate proc, JoeNuvo is right.
0
 
LVL 1

Author Comment

by:papuleo
ID: 34993631
There is no problem with the smGetMinDate.
What confuses me - is that we've been using GP Dynamics for several years and create tickets all the time from within Dynamics itself - this problem only shows up when I call it from outside GP.

I have found at lease one other stored procedure that has similar code.  Most of the other's have code that includes the space.

The only way I can see that piece of code working - would be if the implicit conversion to data type DateTime occured on the two text addends prior to the addition operation.  Such an implicit conversion on those text data types would only occur because theres an assignment (=) operation to a different data type, and I can find nothing that indicates this would happen prior to the addition operation.

This does occur when the two addends are different data types and I did find a good description of such an implicit conversion. (see end of post).

I thought I was just overlooking some simple setting, but I've already tried changing combinations of related parameters such as SET ANSI_DEFAULTS and SET IMPLICIT_TRANSACTIONS to no avail.

We want to have a portable solution for obvious marketing reasons - but I'll just create a new stored procedure within our test GP database so we can continue development and open a ticket with Microsoft.

 ---
 Implicit conversion example description:
" The DateTime data type consists of two Word values for a total size of 8 Bytes (1 Word = 4 Bytes).  The first Word contains the number of days offset from the base date of January 01 1900.  The second Word contains the number of "clock ticks" (3.33 ms time increments) since midnight.
When we run the query GETDATE() + 2 we are actually adding a DateTime data type to an Integer data type.
The DateTime data type has higher precedence so the Integer number will be implicitly converted to the data type of DateTime before the Add operation is performed."
0
 
LVL 10

Expert Comment

by:Abdulmalek_Hamsho
ID: 34993708
I meant that your code:

SET DATEFORMAT YMD
declare @enttme datetime = NULL
declare @MinDate datetime

/* sets the MinDate to the base date of 1900-01-01 */
exec smGetMinDate @MinDate output

if @enttme is NULL   begin
      select @enttme = CONVERT(char(10),@MinDate,102) + CONVERT(char(5),GETDATE(),108)  
end


is completely correct if you changed char(10) to char(11). What's the problem? Cannot you run this SQL statement t your side?
declare @MinDate datetime
exec smGetMinDate @MinDate output

0
 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 500 total points
ID: 34993765
Maybe Dynamics does not call this specific stored procedure at all. Maybe it's there for some historical reasons, or Microsoft internal reasons, etc. Or, since we don't see the full procedure, maybe this piece in some IF... THEN branch that is never kicks in when it's called by Dynamics. You can run SQL  Server Profiler, specify filtering for this stored procedure, and see if it's called while you are working with Dynamics. Then you will see how it's called as well.
0
 
LVL 1

Author Comment

by:papuleo
ID: 34994492
Thanks for the answer about the profiler - you are correct that it actually wasn't being called, and it helped me find the other stored procedures that were being used.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now