FatalExceptionError
Technical User
Problem
"Syntax error converting datetime from character string."
I know what this means and I even know the exact record causing this problem. What I can't figure out is why I am getting this error.
Details
The linecode is a char field but the application that populates it defaults it to a string like yyyymmdd based on the current date. It has to manually be changed by a user.
Kicker #1
The record/s in question do not satisfy any of the parameters in the where clause. This means it technically is not a data issue because it is a legit entry (for another customer) This leads me to believe the profiler is executing a table scan or using a index and then it hits that final clause in the procedure and BOOM.
Kicker #2
The results are not consistent across my environments. This is my real concern. Dev and QA do not throw this error but production does (yay). My first guess is that an index is being used in the other two environments most likely due to different statistics because of actual usage. My other guess is there is a setting in SQL server that might be different but I highly doubt that. The DB and server team stay on top of consistent settings for compliance reasons. The only other difference is in hardware and I used (option maxdop 1) with the same result.
Questions
1. Can someone enlighten me on anything else going on that might cause this?
2. Is there someway for me to handle string data before the cast?
Code
Actual code changed to something closer to plain english. I can post the exact code for the stored proc if someone thinks it is needed but I am pretty sure the details outlined should suffice. Bold code is where I believe my error is happening.
=======================================
The statement below is true
The statement above is false
There are 10 kinds of people, those who know binary and those who don't
"Syntax error converting datetime from character string."
I know what this means and I even know the exact record causing this problem. What I can't figure out is why I am getting this error.
Details
The linecode is a char field but the application that populates it defaults it to a string like yyyymmdd based on the current date. It has to manually be changed by a user.
Kicker #1
The record/s in question do not satisfy any of the parameters in the where clause. This means it technically is not a data issue because it is a legit entry (for another customer) This leads me to believe the profiler is executing a table scan or using a index and then it hits that final clause in the procedure and BOOM.
Kicker #2
The results are not consistent across my environments. This is my real concern. Dev and QA do not throw this error but production does (yay). My first guess is that an index is being used in the other two environments most likely due to different statistics because of actual usage. My other guess is there is a setting in SQL server that might be different but I highly doubt that. The DB and server team stay on top of consistent settings for compliance reasons. The only other difference is in hardware and I used (option maxdop 1) with the same result.
Questions
1. Can someone enlighten me on anything else going on that might cause this?
2. Is there someway for me to handle string data before the cast?
Code
Actual code changed to something closer to plain english. I can post the exact code for the stored proc if someone thinks it is needed but I am pretty sure the details outlined should suffice. Bold code is where I believe my error is happening.
Code:
SELECT
header.document,
header.site,
header.order,
lines.line_status,
lines.line_number ,
CAST(lines.line_code AS DATETIME) AS 'Date',
lines.product,
lines.description,
lines.line_status,
lines.line_status_description,
ISNULL(lines.license,''),
ISNULL(lines.qty, 0),
product.product_description
FROM
header WITH (NOLOCK)
INNER JOIN lines WITH (NOLOCK) ON lines.document = header.document
LEFT JOIN transactions WITH (NOLOCK) ON transactions.line_number = lines.line_no AND transactions.document = lines.document
INNER JOIN status WITH (NOLOCK) ON status.status_code = lines.status_code
INNER JOIN products WITH (NOLOCK) ON lines.product_code = products.product_code
WHERE
customer = @customer
AND
facility = @facility
AND
lines.line_transaction = 'receipt'
AND
lines.line_status = 'received'
AND
header_status <> 'received'
AND
(order LIKE 'xyz%' OR order LIKE 'zyx%')
[b]AND
CAST(lines.line_code AS DATETIME) BETWEEN @begin_date AND @end_date[/b]
=======================================
The statement below is true
The statement above is false
There are 10 kinds of people, those who know binary and those who don't