Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Microsoft SQL not converting text to DateTime before adding values.

Posted on 2011-02-26
14
Medium Priority
?
1,283 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
[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
  • 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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
 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

610 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