If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Issue with BETWEEN MONTH() and MONTH() query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-05, 17:30
impactorange impactorange is offline
Registered User
 
Join Date: Sep 2005
Posts: 1
Issue with BETWEEN MONTH() and MONTH() query

Hi there,

I am trying to select records which have start and end dates that span or include any given month.

I came up with this query but for some reason it isn't giving me the results I expected:

SELECT * FROM Orders WHERE stationid = '26' AND productid = '1' AND ((now() BETWEEN startdate AND enddate) OR ('11' BETWEEN MONTH(startdate) AND MONTH(enddate))) ORDER BY productid

'startdate' and 'enddate' are stored in the DB as timestamp(14) fields.

In this particular case, month = 11 (Nov) but it still retrieves orders with a startdate = 20050901000000 (Sept) and enddate = 20051005000000 (Oct).

I have been beating myself over the head trying to figure out how to pull out records if they happen in a specific month, and I thought I finally had, until I realized this was happening.

Any help would be appreciated. Thanks.
Reply With Quote
  #2 (permalink)  
Old 09-28-05, 03:58
felixg felixg is offline
Registered User
 
Join Date: Apr 2005
Location: Lier, Belgium
Posts: 122
Quote:
Originally Posted by impactorange
SELECT * FROM Orders WHERE stationid = '26' AND productid = '1' AND ((now() BETWEEN startdate AND enddate) OR ('11' BETWEEN MONTH(startdate) AND MONTH(enddate))) ORDER BY productid

In this particular case, month = 11 (Nov) but it still retrieves orders with a startdate = 20050901000000 (Sept) and enddate = 20051005000000 (Oct).
If NOW() returns the timestamp of now (around 2005-09-28), your
'now() BETWEEN startdate AND enddate' subclause, which is ORed with your month selection logic, is true.

--
felix
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On