# Bad sql statement

**URL:** <https://www.epiusers.help/t/bad-sql-statement/74855>\
**Category:** Epicor ERP 10\
**Tags:** e10\
**Created:** [January 26, 2021, 6:35am UTC](https://www.epiusers.help/t/bad-sql-statement/74855 "2021-01-26T06:35:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![emnzava](https://www.epiusers.help/user_avatar/www.epiusers.help/emnzava/32/41803_2.png) [@emnzava](https://www.epiusers.help/u/emnzava)\
**Post date:** [January 26, 2021, 6:35am UTC](https://www.epiusers.help/t/bad-sql-statement/74855/1 "2021-01-26T06:35:20Z")

</div>

Hello Team,  
May i know main reasons for BAD SQL STATEMENT error in BAQ report? I suspect there is more than one. What if there are lots of data and join is OK? Same query when copied in SQL Server run perfectly.

Regards,

---

<div class="post-metadata">

**Author:** ![adaniell](https://www.epiusers.help/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@adaniell](https://www.epiusers.help/u/adaniell)\
**Post date:** [January 26, 2021, 2:00pm UTC](https://www.epiusers.help/t/bad-sql-statement/74855/2 "2021-01-26T14:00:32Z")

</div>

There are many potential reasons, does the BAQ(s) behind the report execute w/o issue?

---

<div class="post-metadata">

**Author:** ![ckrusen](https://www.epiusers.help/user_avatar/www.epiusers.help/ckrusen/32/50584_2.png) [@ckrusen](https://www.epiusers.help/u/ckrusen)\
**Post date:** [January 26, 2021, 2:10pm UTC](https://www.epiusers.help/t/bad-sql-statement/74855/3 "2021-01-26T14:10:57Z")

</div>

Not sure if it would present as “BAD SQL STATEMENT”, but a common problem that seems to pop-up on reports that normally run fine, is when a calculated field has a divide by zero. Maybe 99.9% of your data results in the divisor never being zero. But that 0.1% of the time, there would be an error.

Run the report again with different data (use different inputs (different date, customer, part, class, etc…). If it runs okay, then narrow it down to what is in your data that is causing the problem. Is it a specific date range, customer, part, Invoice Num, etc…

---

<div class="post-metadata">

**Author:** ![rabello](https://www.epiusers.help/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@rabello](https://www.epiusers.help/u/rabello)\
**Post date:** [October 26, 2021, 1:45pm UTC](https://www.epiusers.help/t/bad-sql-statement/74855/4 "2021-10-26T13:45:14Z")

</div>

This is a little late, but in case someone ends up here through a search engine like I did, in addition to the division by zero error I have also gotten the Bad SQL Statement for:

1. Null values in calculated fields - I fix these by using an if/then to set the value if the field is null (example: (case when SubQuery6.Calculated\_cost is null then 0 else SubQuery6.Calculated\_cost end)
2. There is a data type/mismatch in your dataset. I had this one time when I had a date field and one job’s start date value was actually a date/time. We also had one that was imported with 0220 as the year instead of 2020, which wasn’t recognized as a valid date.
3. Your nvarchar field is not long enough for some of the field values. It doesn’t recognize this as a problem until it tries to run the data through it.
4. One of your nvarchar calculated fields has an integer in it. You have to use the conversion to make the integer a string - this usually happens for PO or Sales Order numbers, lines and releases. (example: convert(varchar, OrderRel.OrderNum))

---

<div class="post-metadata">

**Author:** ![emnzava](https://www.epiusers.help/user_avatar/www.epiusers.help/emnzava/32/41803_2.png) [@emnzava](https://www.epiusers.help/u/emnzava)\
**Post date:** [October 30, 2021, 5:39pm UTC](https://www.epiusers.help/t/bad-sql-statement/74855/5 "2021-10-30T17:39:45Z")

</div>

Thanks Rabello,  
For my case, Bad SQL Statement was a result of null value in the join statement. I added a criteria to prevent null values and problem solved.

Regards

---

<div class="post-metadata">

**Author:** ![smason](https://www.epiusers.help/user_avatar/www.epiusers.help/smason/32/3819_2.png) [@smason](https://www.epiusers.help/u/smason)\
**Post date:** [April 18, 2022, 11:41am UTC](https://www.epiusers.help/t/bad-sql-statement/74855/6 "2022-04-18T11:41:45Z")

</div>

We had the problem because of a BAQ where we were joining Memos to POs. Memo Key1 is string and PO is integer so we had to write a convert function to fix the issue.

 ![image](https://www.epiusers.help/uploads/default/original/3X/6/d/6d6193e80a8f6977cdfbd066047bd2a1324b8217.png)
