• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1285
  • Last Modified:

Microsoft SQL not converting text to DateTime before adding values.


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
papuleo
Asked:
papuleo
  • 4
  • 2
  • 2
  • +3
1 Solution
 
dougaugCommented:
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
 
JoeNuvoCommented:
I think you need to add space

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

Open in new window

0
 
papuleoAuthor Commented:
Unfortunately - this is a GP Dynamics stored procedure and can't be modified.
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
dougaugCommented:
The code you have posted above (SET DATEFORMAT YTD ....) is inside your application or GP Dynamics stored procedure?
0
 
papuleoAuthor Commented:
The SQL code in my post iis directly from the GP Dynamics stored procedure.
0
 
Victoria YudinCommented:
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
 
JoeNuvoCommented:
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
 
Vadim RappCommented:
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
 
Abdulmalek_HamshoCommented:
But there's no problem in the smGetMinDate proc, JoeNuvo is right.
0
 
papuleoAuthor Commented:
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
 
Abdulmalek_HamshoCommented:
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
 
Vadim RappCommented:
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
 
papuleoAuthor Commented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now