0

I have two queries that return the same data, only the calculating field with the case is moved. I am using the Access Client Solutions "run query" to execute the query.

If I run this one

Select storenum, businessdate, kitchensenttime, parkcartime as delivertime, (parkcartime-kitchensenttime) as deliveryseconds, case when parkcartime-kitchensenttime>360 then 1 else 0 end as disastrouscount from irislib.tblorder where not(kitchensenttime='01/01/0001' or parkcartime='01/01/0001') and parkcartime>kitchensenttime and businessdate='05/18/2022' 

No rows are returned.

While this one

Select storenum, businessdate, kitchensenttime, case when parkcartime-kitchensenttime>360 then 1 else 0 end as disastrouscount, parkcartime as delivertime, (parkcartime-kitchensenttime) as deliveryseconds from irislib.tblorder where not(kitchensenttime='01/01/0001' or parkcartime='01/01/0001') and parkcartime>kitchensenttime and businessdate='05/18/2022' 

Will return rows. If I try the "broken" query with just the storenum and businessdate with just the disastrouscount calculation will it return rows.

4
  • 3
    I suspect these are not the exact queries that you run, because I see no logical reason for them to behave the way you describe. Commented May 19, 2022 at 16:32
  • @mustaccio I know, those are the queries. Let me see if I can post some screenshots to show one vs the other. ![image.png](postimg.cc/4HySKnqJ) ![image.png](postimg.cc/mcysGz7k) Commented May 20, 2022 at 12:35
  • what version of Db2 for IBM i? Are you current on PTFs? Commented May 20, 2022 at 13:25
  • I had to ask, 7.3 OS and yes on the PTF Commented May 20, 2022 at 13:58

1 Answer 1

1

I'm with Mustaccio, there's no reason moving the calculated field should cause the rows to change.

I did notice that the statement being run in the working screenshot appears to be just the statement you're expecting... enter image description here

whereas in the broken, you've picked up some commented out stuff first... enter image description here

I also notice you've not terminated any statements, with a ;. I'd suggest you do so. As the only explanation I can come up with is that you're picking up some code you don't intended to. On the Messages tab of Run SQL Scripts, take a look at the statement actually being run. enter image description here

In addition, you should always use ISO date/time format for literals.

It appears that kitchensenttime and parkcartime are timestamps, not dates. Comparing them to a date forces the DB to implicitly convert either the column value or the literal. It's better practice to explicitly do the conversions yourself.

Furthermore, '0001-01-01' is suspect, if the column is nullable, it's more likely that the columns are null and don't actually contain '0001-01-01-00:00:00.000000'. If the columns are defined NOT NULL and the records are being written out using RPG op-codes, then RPG is defaulting in the '0001-01-01-00:00:00.000000'

--having this semicolon will ensure the parser doesn't see -- any prior statements as being a part of this one. ; --try this Select storenum, businessdate, kitchensenttime, parkcartime as delivertime, (parkcartime-kitchensenttime) as deliveryseconds, case when parkcartime-kitchensenttime>360 then 1 else 0 end as disastrouscount from irislib.tblorder where not(kitchensenttime = timestamp_iso(date('0001-01-01')) or parkcartime = timestamp_iso(date('0001-01-01')) ) and parkcartime>kitchensenttime and businessdate= date('2022-05-18'); 
1
  • Good news bad news. Bad news, I had to reboot my computer. Since I was playing with different things I did not save the query so I can not recreate the issue. I think you and @mustaccio were right. I added the ';' and it threw an error. I pasted your query and it ran. If I put the case on it onto its own line my statement would run. The '0001-01-01' is what a different app uses for a null date. Good to know about the best practice about the timestamp_iso. I guess we are going to have to close this one with an 'I bet you two were right' I can't tell you what exactly I did wrong. Commented May 20, 2022 at 16:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.