IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

SQL query with between for Dates fetches no records


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
rakesh17684

New User


Joined: 08 Oct 2006
Posts: 61
Location: San Diego

PostPosted: Fri Jul 25, 2008 11:33 am
Reply with quote

i have a SQL in which i fetch records based on a date range.
i use a 'between' in the where clause of the query. when the start date is greater than the end date in the query i get no rows but vice versa i get the records i want.

why is that ??

SELECT * FROM table1
WHERE date BETWEEN '2008-01-01' and '2008-02-20'; ---> icon_smile.gif
i get the records

SELECT * FROM table1
WHERE date BETWEEN '2008-02-20' and '2008-01-01' ; ---> icon_sad.gif
No records fetched
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Fri Jul 25, 2008 1:03 pm
Reply with quote

If a date is >= '2008-02-20' it can not be <= '2008-01-01'!
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Jul 25, 2008 2:44 pm
Reply with quote

COL BETWEEN A AND B is equivalent to COL >= A AND COL <= B
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Jul 25, 2008 8:10 pm
Reply with quote

Hello,

Quote:
SELECT * FROM table1
WHERE date BETWEEN '2008-02-20' and '2008-01-01' ; ---> icon_sad.gif
No records fetched
The time to be concerned would be if any rows were returned. . . icon_smile.gif
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Sat Jul 26, 2008 5:13 am
Reply with quote

Don't confuse the square root of -1 with SQL, Dick. icon_lol.gif
Back to top
View user's profile Send private message
vini_srcna

Active User


Joined: 26 May 2005
Posts: 178
Location: Copenhagen, Denmark

PostPosted: Mon Jul 28, 2008 6:17 pm
Reply with quote

Thats how it is defined to work icon_smile.gif
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Fri Aug 01, 2008 11:20 am
Reply with quote

It won't Display any row if you execute the query like
SELECT * FROM table1
WHERE date BETWEEN '2008-02-20' and '2008-01-01'

The generic example using between is like select.... where <col> BETWEEN 1st value and 2nd value

Because for BETWEEN function 1 the 1st value should be low as it selects the only those values whcih satisfies the criteria value >= 1st Value and value =< 2nd value and then fetches the records

In your case there will no such records will come as it will search for dates where Date >= '2008-02-20' and Date =< '2008-01-01'

Reagrds,
Chandan
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 3
No new posts Compare 2 files(F1 & F2) and writ... JCL & VSAM 8
No new posts Compare only first records of the fil... SYNCSORT 7
No new posts Pulling a fixed number of records fro... DB2 2
No new posts RC query -Time column CA Products 3
Search our Forums:

Back to Top