English 中文(简体)
DB2 ZOS String Comparison Problem
原标题:
  • 时间:2010-05-05 14:32:35
  •  标签:
  • c#
  • db2
  • zos

I am comparing some CHAR data in a where clause in my sql like this,

where PRI_CODE < PriCode

The problem I am having is when the CHAR values are of different lengths. So if PRI_CODE = 0800 and PriCode = 20 it is returning true instead of false.

It looks like it is comparing it like this

 08  <  20  

instead of like

 0800  <  20 

Does a CHAR comparison start from the Left until one or the other values end?

If so how do I fix this?

My values can have letters in it so convering to numeric is not an option.

最佳回答

It s not comparing 08 with 20 , it is, as you expect, comparing 0800 with 20 .

What you don t seem to expect, however, is that 0800 (the string) is indeed less than 20 (the string).

If converting it to numerics for a numeric comparison is out of the question, you could use the following DB2 function:

right ( 0000000000 ||val,10)

which will give you val padded on the left with zeroes to a size of 10 (ideal for a CHAR(10), for example). That will at least guarantee that the fields are the same size and the comparison will work for your particular case. But I urge you to rethink how you re doing things: per-row functions rarely scale well, performance-wise.

If you re using z/OS, you should have a few DBAs just lying around on the computer room floor waiting for work - you can probably ask one of them for advice more tailored to your specific application :-)

One thing that comes to mind in the use of an insert/update trigger and secondary column PRI_CODE_PADDED to hold the PRI_CODE column fully padded out (using the same method as above). Then make sure your PriCode variable is similarly formatted before executing the select ... where PR_CODE_PADDED < PriCode.

Incurring that cost at insert/update time will amortise it over all the selects you re likely to do (which, because they re no longer using per-row functions, will be blindingly fast), giving you better overall performance (assuming your database isn t one of those incredibly rare beasts that are written more than read, of course).

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签