Solved

Microsoft SQL not converting text to DateTime before adding values.

Posted on 2011-02-26
14
1,274 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Automatically creating a Trello card using data from a Microsoft Dynamics CRM record turned out to be an easy project that yielded great results.  Here's how I did this for an internal team at General Code.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

14 Experts available now in Live!

Get 1:1 Help Now