Solved

Query where date and time are in separate fields

Posted on 2009-07-09
9
218 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_
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

This article describes some very basic things about SQL Server filegroups.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

830 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