Question

as 400 date/time query

Asked by: whsrptg

hi all,

I'm trying to run a query from VBA, which I will be moving to VB6 once it's installed on my work PC. basically I need to get some records between 2 dates.

the code works as far as getting the right date time for starting and ending. but when the query runs against the AS/400 I dont get anything back. below are some examples of what's being sent and what is in the database itself.

sending:
Start Date/Time: 2009-08-30-17:01:00.0000000
End Date/Time: 2009-08-31-12:00:00.0000000

Actual data from database: 2009-08-30-21.27.21.000000 and 2009-08-30-21.47.41.000000

both of which fall between the date/times sent.

I'm at a loss how to do this. I've never really worked with the AS/400 but have done a lot with sql server and the dreaded Access sql.

NewStart_DateTime = Format(NewStart_DateTime & " " & Left(CODAM1, StartPosition) & ":" & Right(CODAM1, 2) & ":00", "mm/dd/yyyy hh:nn:ss AMPM")
NewStart_DateTime = DateAdd("n", -(CODAM3), NewStart_DateTime)
NewStart_DateTime = Format(NewStart_DateTime, "yyyy-MM-dd-hh:mm:ss.0000000")
        
If Len(CODAM2) = 3 Then
       StartPosition = 1
Else
       StartPosition = 2
End If
 
NewEnd_DateTime = Format(CurrentSheet.Cells(4, 2) & " " & Left(CODAM2, StartPosition) & ":" & Right(CODAM2, 2) & ":00", "mm/dd/yyyy hh:nn:ss AMPM")
NewEnd_DateTime = DateAdd("n", CODAM3, NewEnd_DateTime)
NewEnd_DateTime = Format(NewEnd_DateTime, "yyyy-MM-dd-hh:mm:ss.0000000")
        
GetEmployee_SQL = " SELECT cpttst, cptopc, cptclc " & _
                            " FROM CPETT00 " & _
                            " WHERE cptemp= '" & Neweeempn & "' " & _
                            " And date(timestamp('" & NewStart_DateTime & "')) <= date(timestamp(CPTTST)) " & _
                            " And date(timestamp('" & NewEnd_DateTime & "')) >= date(timestamp(CPTTST)) " & _
                            " ORDER BY CPTTST "

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-31 at 11:37:06ID24695684
Topics

Programming for iSeries / AS400

,

DB2 Database

Participating Experts
2
Points
500
Comments
21

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Connecting to an AS/400
    I'm trying to connect to an AS/400 from a VB app so I can run some queries. I've been able to connect fine, but whenever I try to execute a SQL statement I get the error "Method 'Execute' of object '_Connection' failed". Here is the code I'm using: ---------------...
  2. AS/400 SQL Date Syntax
    Hi all, I have a quick question about the syntax for an SQL statement I am writing. I need to know how to do an UPDATE statement such as: UPDATE U269672.MYFILE SET [Date Created] = .....(a value that an AS/400 file recognizes as a date).....WHERE.....(rest of code I know)....
  3. Coldfsuion with AS/400
    Question 1 ======= When my datasource was pointing to a MS SQl I could use the SETROWCOUNT. Example SET ROWCOUNT 100; SELECT * FROM FILE1 Now that I have my datasource pointing to our AS/400 it doe not seem to recognize the SETROWCOUNT. What is the equivalent of SETROWCOU...
  4. A VB6 / Access SQL Query falls over on inner Join but n…
    Hi all I am pretty new to VB6 so this is probably quite simple but it is confusing the hell out of me. I am attempting to build an products browser which connects to Microsoft Access. The tables which hold the information are relational (I never designed them - I inherited ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: KdoPosted on 2009-08-31 at 12:00:13ID: 25225356

Hi whsrptq,

The date portion of the query's filter looks reasonable.  (It may be easier to read if you code it using BETWEEN, but that won't affect the results.)

Are you sure that the sample data that you show is being selected with a correct value for cptemp?


Kent

 

by: whsrptgPosted on 2009-08-31 at 12:04:23ID: 25225396

yes I am. I had the query run without the 'and' conditions for the date and that is what was returned. The table is very small at the moment since we haven't installed to production yet.

 

by: Gary_The_IT_ProPosted on 2009-08-31 at 12:05:12ID: 25225407

Well,

First of all, if I read this right, you are looking for records with a date that is less than or equal to your start time AND simultaneously greater than or equal to your end time.  Unlikely you will find many dates that fit that test.

Second,

A timestamp contains three  components: a DATE, a TIME, and microseconds.

Assuming CPTTST is defined on the AS/400 as a TIMESTAMP, you won't want to sue the date() function at all, since it just strips the date portion (year, month, day) out of a timestamp (year, month, day, hours, minute, second, microsecods)  

If your goal is to use a date  plus a time for your extract, then you just need to compare a properly formatted quoted string to the timestamp field.

- Gary Patterson

Check out my EE profile: http://www.experts-exchange.com/M_4382324.html

'2009-02-02-13.17.23.000000' <= CPTTST

                                              
1:

Select allOpen in new window

 

by: Gary_The_IT_ProPosted on 2009-08-31 at 12:09:57ID: 25225455

Also note that in and ISO timestamp, the separator in the time portion of the timestamp is "." (period), not ":" (colon).  I get an error when I try to substitute colons on a native query on the AS/400,

- Gary Patterson

Check out my EE profile: http://www.experts-exchange.com/M_4382324.html

 

by: whsrptgPosted on 2009-08-31 at 12:14:17ID: 25225490

" And date(timestamp('" & NewStart_DateTime & "')) <= date(timestamp(CPTTST)) " & _
" And date(timestamp('" & NewEnd_DateTime & "')) >= date(timestamp(CPTTST)) " & _

states that the start time is less than the database field and the end date is greater than the database field.  

Taking the date portion off of  " And date(timestamp('" & NewEnd_DateTime & "'))" causes an error "SQL0180: Syntax of date, time or datestamp value not valid"

 

by: KdoPosted on 2009-08-31 at 12:16:06ID: 25225502

Hi Gary,

Looks like you read it wrong. :)

" And date(timestamp('" & NewStart_DateTime & "')) <= date(timestamp(CPTTST)) " & _
" And date(timestamp('" & NewEnd_DateTime & "')) >= date(timestamp(CPTTST)) " & _


Hi Hi whsrptq,

DB2 has some pretty power date manipulation routines (and syntax). There's no need to jump through all of these hoops when you don't have to. You don't have to build and pass an entire datetime only to recast it to a date. You can build the date and pass it. You can also set the range within the query instead of having to build and pass another date.

" And date('" & NewStart_Date & "') <= date(CPTTST) " & _
" And date('" & NewEnd_Date & "') >= date(CPTTST) " & _

or
" And '" & NewStart_Date & "' <= date(CPTTST) " & _
" And '" & NewEnd_Date & "' >= date(CPTTST) " & _

or even
" And date (CPTST) BETWEEN '" & NewStart_Date & " AND " & NewEnd_Date & "' _


I've probably misplaced a quote, but it's close. :)

Kent





 

by: whsrptgPosted on 2009-08-31 at 12:37:26ID: 25225669

Using the date function causes the same error.  "SQL0180: Syntax of date, time or datestamp value not valid"

GetEmployee_SQL = " SELECT cpttst, cptopc, cptclc " & _
                  " FROM CPETT00 " & _
                  " WHERE cptemp= '" & Neweeempn & "' " & _
                  " And date(timestamp(CPTTST)) BETWEEN '" & NewStart_Date & "' AND '" & NewEnd_Date & "' " & _
                  " ORDER BY CPTTST "

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: whsrptgPosted on 2009-08-31 at 12:39:38ID: 25225692

oops pasted the wrong code

GetEmployee_SQL = " SELECT cpttst, cptopc, cptclc " & _
                  " FROM CPETT00 " & _
                  " WHERE cptemp= '" & Neweeempn & "' " & _
                  " And date(CPTTST) BETWEEN '" & NewStart_Date & "' AND '" & NewEnd_Date & "' " & _
                  " ORDER BY CPTTST "

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: KdoPosted on 2009-08-31 at 13:01:46ID: 25225907

Ok.  can you post the value of CPTTST?  

 

by: whsrptgPosted on 2009-08-31 at 13:04:23ID: 25225939

Actual data from database (cpttst):

2009-08-30-21.25.49.000000

2009-08-31-01.30.00.000000

2009-08-31-02.03.00.000000

2009-08-31-05.58.43.000000

 

by: KdoPosted on 2009-08-31 at 13:15:04ID: 25226039

DB2 is sooooo picky....

A datetime (at least on other flavors of DB2) has a period between the date and time portions, not a hyphen.  That's what's causing the date-time error.

So a substr() of the first 10 characters will return '2009-08-30'.  If your instance of DB2 is configured with that as the default date, perfect.  If not, you'll need to format the date (in php) in the correct format, probably '2009/08/30' or '08/30/2009'.


Kent

GetEmployee_SQL = " SELECT cpttst, cptopc, cptclc " & _
                  " FROM CPETT00 " & _
                  " WHERE cptemp= '" & Neweeempn & "' " & _
                  " And date(substr(CPTTST,1,10)) BETWEEN '" & NewStart_Date & "' AND '" & NewEnd_Date & "' " & _
                  " ORDER BY CPTTST "
                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: Gary_The_IT_ProPosted on 2009-08-31 at 13:19:22ID: 25226077

Kent,

Yep, read it wrong.  I prefer your BETWEEN syntax: much clearer to me.

whsrptq:

I think that the SQL0180 is because the syntax of your TIMESTAMP constant in the SQL is not correct:

NewStart_DateTime = Format(NewStart_DateTime, "yyyy-MM-dd-hh:mm:ss.0000000")
NewEnd_DateTime = Format(NewEnd_DateTime, "yyyy-MM-dd-hh:mm:ss.0000000")

Swap the ":"s for "."s and see if that doesn't resolve the syntax error.

Do you want your selection criteria to include a date and time, or just a date?  IF so, you need to drop those DATE() references and just compare the TIMESTAMP variable to a valid TIMESTAMP literal.

Again, how is

- Gary Patterson

Check out my EE profile: http://www.experts-exchange.com/M_4382324.html


and CPTTST BETWEEN '2009-02-17-01.02.03.000000' AND '2009-02-18-06.32.17.000000'
 
(Assumes CPTTST is declared as TIMESTAMP (is it?)
                                              
1:
2:
3:

Select allOpen in new window

 

by: Gary_The_IT_ProPosted on 2009-08-31 at 13:23:28ID: 25226118

iSeries DB2 TIMESTAMP (not DATETIME) uses a hyphen to seperate date and time portions, as shown in the example data above.  The colons are the problem.

- Gary

 

by: KdoPosted on 2009-08-31 at 13:25:06ID: 25226134

So many things to learn...

So little time.....

 

by: whsrptgPosted on 2009-08-31 at 13:32:34ID: 25226218

HAH!

in typing an updated section to this, my original code was

NewStart_DateTime = Format(NewStart_DateTime, "yyyy-MM-dd-hh:mm:ss.0000000")
2009-08-30-17:01:00.0000000

the as 400 value was  

2009-08-30-21.25.49.000000

the as/400 value has 1 less zero on the end. making the code read

NewStart_DateTime = Format(NewStart_DateTime, "yyyy-MM-dd-hh:mm:ss.000000")

2009-08-30-17:01:00.000000

fixes the issue. OMG i never thought it would be THAT picky.

 

by: whsrptgPosted on 2009-08-31 at 13:34:11ID: 25226231

so the new and correct code is.

NewStart_DateTime = Format(NewStart_DateTime & " " & Left(CODAM1, StartPosition) & ":" & Right(CODAM1, 2) & ":00", "mm/dd/yyyy hh:nn:ss AMPM")
NewStart_DateTime = DateAdd("n", -(CODAM3), NewStart_DateTime)
NewStart_DateTime = Format(NewStart_DateTime, "yyyy-MM-dd-hh.mm.ss.000000")
 
NewEnd_DateTime = Format(CurrentSheet.Cells(4, 2) & " " & Left(CODAM2, StartPosition) & ":" & Right(CODAM2, 2) & ":00", "mm/dd/yyyy hh:nn:ss AMPM")
NewEnd_DateTime = DateAdd("n", CODAM3, NewEnd_DateTime)
NewEnd_DateTime = Format(NewEnd_DateTime, "yyyy-MM-dd-hh.mm.ss.000000")
 
GetEmployee_SQL = " SELECT cpttst, cptopc, cptclc " & _
                  " FROM CPETT00 " & _
                  " WHERE cptemp= '" & Neweeempn & "' " & _
                  " And date(CPTTST) BETWEEN date(timestamp('" & NewStart_DateTime & "')) AND date(timestamp('" & NewEnd_DateTime & "')) " & _
                  " ORDER BY CPTTST "

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

Select allOpen in new window

 

by: Gary_The_IT_ProPosted on 2009-08-31 at 13:36:01ID: 25226255

Good job!

- Gary

 

by: KdoPosted on 2009-08-31 at 13:36:05ID: 25226256

I feel really, really bad about this.

I've corrected quite a few people that only had 5 digits for the millisecond clock, but completely missed the fact that this code had 7.  :/

Glad that you spotted it!

Kent

 

by: whsrptgPosted on 2009-08-31 at 13:38:16ID: 25226278

no worries. i'm just glad i found it. i was beginning to think i didnt have a clue!

 

by: Gary_The_IT_ProPosted on 2009-08-31 at 14:28:36ID: 25226713

Seems inappropriate to delete the question when we solved:

Misuse of the date() function,
Syntax of the timestamp separators.

Not to mention that I also explained the three components of the timestamp field, including the MICROSECONDS field, which, by definition is 10^-6?

I'm fairly sure that Kent's explanation of how precise you need to be with the timestamp at least assisted you in your solution.

- Gary



 

by: Gary_The_IT_ProPosted on 2009-08-31 at 14:28:43ID: 25226714

Seems inappropriate to delete the question when we solved:

Misuse of the date() function,
Syntax of the timestamp separators.

Not to mention that I also explained the three components of the timestamp field, including the MICROSECONDS field, which, by definition is 10^-6?

I'm fairly sure that Kent's explanation of how precise you need to be with the timestamp at least assisted you in your solution.

- Gary



20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...