Solved

Query where date and time are in separate fields

Posted on 2009-07-09
9
222 Views
Last Modified: 2013-12-25
Basically, each record in my table has a date and time but the date and time are in different columns. I want to select all records after a single point in time (the fields are on an AS400 stored as "Zoned" datatypes, which is basically like a float).

It won't work to do:
select *
from myTable
where date >= 20090101
and time >= 222222
...because this would exclude a record with a date of 20090102 but that a time of 111111. I want ALL records with a date/time greater than 20090101/222222. This should include records with a time < 222222 as long as the date was greater than or equal to 20090102.

My intuition is that I need to do a query that returns all dates >= myDate, then, in the result set, Select * if (date=myDate AND time=myTime) OR (date>myDate).

I have no idea how to represent this in SQL Syntax.
0
Comment
Question by:_Wade_
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24814774
What form is your time in? I'm using what you provided.


select * from myTable where date >=  convert(datetime, '20090101', 112)
   and time >= 222222
;


However, can you clarify the types of those columns and what is stored exactly?
0
 

Author Comment

by:_Wade_
ID: 24814793
The fields are on an AS400 stored as "Zoned" datatypes, which is basically like a float.
0
 
LVL 42

Accepted Solution

by:
pcelba earned 250 total points
ID: 24814801
Several possible ways exist.

select *
from myTable
where (date = 20090101 and time >= 222222) or date > 20090101

Or you could convert date and time to one text string and compare it as a string.

select *
from myTable
where STR(date) + STR(time) >= "20090101222222"

I am not sure about the exact STR function availability, syntax and its output. It depends on your SQL engine. You have to test it for times less than 120000 because the missing digit would cause another problems.
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:_Wade_
ID: 24814817
One more thing...

My goal is to put the sql query into an SSIS package while will use ODBC to execute the command against the AS400 DB. But, I'm practicing the command natively on the as400 command line.
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24814841
>>.because this would exclude a record with a date of 20090102 but that a time of 111111. I want ALL records with a date/time greater than 20090101/222222. This should include records with a time < 222222 as long as the date was greater than or equal to 20090102.

This does not make sense.

If you want that, then ignore the time column altogether.



>>I want ALL records with a date/time greater than 20090101/222222. This should include records with a time < 222222

That does not compute. 20090101/111111 is not greater than 20090101/222222, but you say you want to include them. I think maybe your logic was misworded here?

0
 
LVL 42

Expert Comment

by:pcelba
ID: 24815068
The first query mentioned in my answer ID:24814801 should work for you independently on the way of data retrieving.

The STR() function usage you have to check from AS400 command line.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24815209
You may ask for data type conversions here at EE in a zone especially derived for AS400 (http://www.experts-exchange.com/Programming/System/AS_-_400/)

AS400 experts should know better what everything is available for you.
0
 

Author Comment

by:_Wade_
ID: 24815994
mrjoltcola:

>>I want ALL records with a date/time greater than 20090101/222222. This should include records with a time < 222222

That does not compute. 20090101/111111 is not greater than 20090101/222222, but you say you want to include them. I think maybe your logic was misworded here?

Maybe few more words would have clarfied:
"This should include records with a time < 222222 [as long as the date for those records is greater than 20090101]..."

0
 

Author Closing Comment

by:_Wade_
ID: 31601662
Worked great!!!
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

734 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