Solved

DB2 SQL Syntax

Posted on 2010-08-27
20
1,397 Views
Last Modified: 2012-05-10
I get the following error message when I try to run the query below:

ODBC - Call Failed.

[IBM][CLI Driver][DB2] SQL0420N Invalid character found in a character string argument of the function "n + e". SQL State = 22018 (#-420)

Can anyone tell me what's wrong with the WHERE clause in the query below. Without the where clause, the query works perfectly.
SELECT CInt(Replace(T162_CGY_SHT_NME,'DC','')) AS [Facility Number], IIf(T627_FRG_TER_CD=5 Or T627_FRG_TER_CD=6,'Import','Domestic') AS [Current Sourcing], IIf((T627_FRG_TER_CD=5 Or T627_FRG_TER_CD=6) And T1085_CAC_MTH_ID<>'O','Y','N') AS [Current Import with OTR Freight], IIf([Facility Number] IN (955,960,961,962,965,966,990,992,1419,1420,1421,1436,1440,1449),'RDC',IIf([Facility Number]=964,'Trap',IIf([Facility Number] IN (1438,1439,1446),'Transload','Store'))) AS [Current Flow Path], IIf(T605_RP_TYP_ID='OXD',IIf(OXD_SWT_1XD_IDC='Y','1XD','Stock'),T605_RP_TYP_ID) AS [Current RDC Flow Type], IIf(T627_FRG_TER_CD In (1,4,5),'Collect','Prepaid') AS [Current Freight Terms], IIf([Current Sourcing]='Import', IIf([Current Primary Constraint Type]='Cube', IIf([Current Primary Max Constraint]>=2732,'45''',IIf([Current Primary Max Constraint]>=2362,'40HC''',IIf([Current Primary Max Constraint]>=2081,'40''',IIf([Current Primary Max Constraint]>=1025,'20''','N/A')))),IIf([Current Secondary Constraint Type]='Cube', IIf([Current Secondary Max Constraint]>=2732,'45''',IIf([Current Secondary Max Constraint]>=2362,'40HC''',IIf([Current Secondary Max Constraint]>=2081,'40''',IIf([Current Secondary Max Constraint]>=1025,'20''','N/A')))),'N/A')),'N/A') AS [Current Container Size], IIf(([Current Primary Constraint Type]='Weight' And [Current Primary Max Constraint]>44000) Or ([Current Secondary Constraint Type]='Weight' And [Current Secondary Max Constraint]>44000),'Y','N') AS [Current Heavy Weight Container], IIf([Current Sourcing]='Import',  IIf([T616_CDN_VBU_NBR]>0,'Yes','No'), 'N/A') AS [Current Oversea Consolidation], DES_TXT AS [Current Freight Type], IIf(IFM_PRM_NBR Is Not Null,IFM_PRM_NBR,IFM_PRM_NBR) AS [Current Start Gate DOS], 0 AS [Current Min Gate], IFM_SRCHFLAG_TXT AS [Current TL Searching], IIf(IFM_HDFLAGTAB_1TXT='V','Cube',IIf(IFM_HDFLAGTAB_1TXT='C','Cost',IIf(IFM_HDFLAGTAB_1TXT='W','Weight',IIf(IFM_HDFLAGTAB_1TXT='K','Cases',IIf(IFM_HDFLAGTAB_1TXT='O','Points',IIf(IFM_HDFLAGTAB_1TXT
='P','Pallets',IIf(IFM_HDFLAGTAB_1TXT='U','Units','Other'))))))) AS [Current Primary Constraint Type], IFM_VRMIN_1_NBR AS [Current Primary Min Constraint], IFM_VRMAX_1_NBR AS [Current Primary Max Constraint],IIf(IFM_HDFLAGTAB_2TXT='V','Cube', IIf(IFM_HDFLAGTAB_2TXT='C','Cost',IIf(IFM_HDFLAGTAB_2TXT='W','Weight',IIf(IFM_HDFLAGTAB_2TXT='K','Cases', IIf(IFM_HDFLAGTAB_2TXT='O','Points',IIf(IFM_HDFLAGTAB_2TXT='P','Pallets',IIf(IFM_HDFLAGTAB_2TXT='U','Units','Other'))))))) AS [Current Secondary Constraint Type], IFM_VRMIN_2_NBR AS [Current Secondary Min Constraint], IFM_VRMAX_2_NBR AS [Current Secondary Max Constraint], 1 AS [Current PO Count Per Order Cycle] FROM qryPassThrough
WHERE INT(REPLACE(LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME,'DC','')) IN (955,960,961,962,965,966,990,992,1419,1420,1421,1436,1440,1449,964,1438,1439,1446)

Open in new window

0
Comment
Question by:ltdanp22
[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
  • 7
  • 6
  • 4
  • +2
20 Comments
 
LVL 18

Expert Comment

by:Dave Ford
ID: 33544051

There's probably some non-numeric data in LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME.
0
 
LVL 18

Accepted Solution

by:
Dave Ford earned 167 total points
ID: 33544096

Look at the data to determine where the bad data is ...


select REPLACE(LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME,'DC','')) 
from   qryPassThrough

Open in new window

0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 33544172
would be easier to try

select *
from   qryPassThrough
where trim(translate(REPLACE(LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME,'DC','')),'          ','0123456789','') ) <> ''
0
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!

 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33544350
That has got to be the ugliest query that I've ever seen.

Still, most everyone here is on the same theme.

Instead of converting the value to an INT in the filter, quote the items in the IN() list.

WHERE REPLACE(LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME,'DC','') IN ('955','960','961','962','965','966','990','992','1419','1420','1421','1436','1440','1449','964','1438','1439','1446')

Your filter will test every row, even those that don't contain 'DC'.  It may be that you only want to test the rows with 'DC', but your filter doesn't do that.


And of course, if there's a real data issue, Momi's last response shows you how to find the row(s) that don't conform to your expectation.


Good Luck,
Kent
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33544721

By the way, that's not DB2 SQL.  It's SQL Server.


But it looks like we've got it covered.  :)


Kent



0
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 167 total points
ID: 33545258
I think the IN() comparisons will be much faster if they remain a list of numeric values.

Likely cause of the error message:
The Replace() function returns a string and needs to be converted to a numeric value.
WHERE CLNG(REPLACE(LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME,'DC','')) IN (955,960,961,962,965,966,990,992,1419,1420,1421,1436,1440,1449,964,1438,1439,1446)

Open in new window

0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 166 total points
ID: 33545361
Hi aikimark,

The CLNG function converts a string to a long.  It looks like REPLACE is returning a non-numeric string somewhere in the process.


Kent
0
 

Author Comment

by:ltdanp22
ID: 33545507
That worked! There's actually a second half to the WHERE clause that I thought was fine but is still causing problems. Any idea what's wrong here?
WHERE REPLACE(LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME,'DC','') IN ('955','960','961','962','965','966','990','992','1419','1420','1421','1436','1440','1449','964','1438','1439','1446') AND LOWES.T556_RP_MP_VBU.T554_RP_NBR=72370 AND LOWES.T556_RP_MP_VBU.T619_MP_NBR=4 AND LOWES.T556_RP_MP_VBU.T616_VBU_NBR=95001 AND LOWES.T748_EFC_HDR_PRM.IFM_PRM_TXT='G';

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 33545539
@Kent

If the output of the Replace() function might not be numeric, convertible into a numeric value, then the VAL() function would work in place of the CLNG().
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33545746
Hi ltdan,

The new filter looks reasonable.  What are you seeing that's wrong?


Kent
0
 

Author Comment

by:ltdanp22
ID: 33546001
If I remove all criteria and add any ONE of the following:
LOWES.T556_RP_MP_VBU.T554_RP_NBR=72370
LOWES.T556_RP_MP_VBU.T619_MP_NBR=4
LOWES.T556_RP_MP_VBU.T616_VBU_NBR=95001
I get the same error message as in the OP. The other two criteria do not cause the error. I've checked the data types of the fields referenced and they are all Long Integer.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 33546033
try the following conditions (without table name qualifier)
T554_RP_NBR=72370
T619_MP_NBR=4
T616_VBU_NBR=95001

Open in new window

0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33546132
The filter looks fine.  Adding any of those 3 lines causes a data conversion exception it's reasonable to assume the data type is not numeric.

0
 

Author Comment

by:ltdanp22
ID: 33558147
When I try other numbers in the criteria, the results are returned as expected. It's just 72370, 4, and 95001 that cause the error.
[IBM][CLI Driver][DB2] SQL0420N Invalid character found in a character string argument of the function "n + e". SQL State = 22018 (#-420)
Does that shed any light on the source of the error? Also, does anyone know why the error says the the argument is a string?
0
 

Author Comment

by:ltdanp22
ID: 33558168
aikimark,
i tried your suggestion and got an error telling me that the field name was not valid in context.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33558580

Try this:

  SELECT T554_RP_NBR
  FROM QryPassThrough
  WHERE T554_RP_NBR like '%72370%'


That will show you all of the rows with the troublesome value.  It should be easy to identify the offending row(s).


Kent


0
 

Author Comment

by:ltdanp22
ID: 33560377
Kent, it didn't like %. Exact error message was an unexpected token '%' was found following ''
0
 
LVL 46

Expert Comment

by:aikimark
ID: 33560411
@ltdanp22

Is this a VB6 application?
Are you going directly to the DB2 database or through an MS-Access database?
 
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 33560549
Hi ltdan,

Let me make sure that I understand.  If you run the query with this filter:

WHERE REPLACE(LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME,'DC','')
        IN ('955','960','961','962','965','966','990','992','1419','1420','1421','1436','1440','1449','964','1438','1439','1446')

It works, but when you add the additional filter clauses:

WHERE REPLACE(LOWES.T572_SHP_LCT_GRP.T162_CGY_SHT_NME,'DC','')
        IN ('955','960','961','962','965','966','990','992','1419','1420','1421','1436','1440','1449','964','1438','1439','1446')
  AND LOWES.T556_RP_MP_VBU.T554_RP_NBR=72370
  AND LOWES.T556_RP_MP_VBU.T619_MP_NBR=4
  AND LOWES.T556_RP_MP_VBU.T616_VBU_NBR=95001
  AND LOWES.T748_EFC_HDR_PRM.IFM_PRM_TXT='G';



It fails?  If the three columns in the filter that end with 'NBR' are numeric, that just can't be.


Kent

0
 

Author Closing Comment

by:ltdanp22
ID: 33581016
This ended up being a data issue after all (not being able to convert CGY_SHT_NME to a number). I spread the points around since everyone said that this was the problem in varying levels of detail.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month6 days, 18 hours left to enroll

623 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