tags:

views:

150

answers:

2

Hi Everyone,

I need help to write one query

Description:

I need to find out the every second Saturday of the each month of the year.

Thanks in advanced,

RPL

+1  A: 

There is a good technique demonstrating how to do this here. Scroll down to Tip #23. It will have you create a small table to facilitate the query. Once you have the table created, your query is something like this:

SELECT monthdate AS first_day_of_month,
    DATE_ADD(monthdate, 
        INTERVAL( ( 10 - DAYOFWEEK(monthdate) ) % 7 ) + 7 DAY) AS second_saturday_of_month
FROM monthdates
RedFilter
Hello orbman, i just go through your pdf and i workout the logic for this. Your answer has little modification. Just see my answer.
Karthik
+1  A: 

Hello riddhi, try this,

This is workout from orbman idea, thanks orbman.

First your table is like this,

CREATE TABLE `monthdates` (
  `monthdate` date NOT NULL,
  PRIMARY KEY  (`monthdate`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

INSERT INTO `monthdates` VALUES ('2010-02-01');
INSERT INTO `monthdates` VALUES ('2010-03-01');
INSERT INTO `monthdates` VALUES ('2010-04-01');
INSERT INTO `monthdates` VALUES ('2010-05-01');
INSERT INTO `monthdates` VALUES ('2010-06-01');
INSERT INTO `monthdates` VALUES ('2010-07-01');

In this table, your providing the first date of month.

Then use this query for second saturday,

    SELECT monthdate AS first_day_of_month, DATE_ADD(monthdate, INTERVAL( ( 14 - DAYOFWEEK(monthdate) ) % 7 ) + 7 DAY) AS second_saturday_of_month, DAYNAME(DATE_ADD(monthdate, INTERVAL( ( 14 - DAYOFWEEK(monthdate) ) % 7 ) + 7 DAY)) as dayy
 FROM monthdates

Output Is :

first_day_of_month  second_saturday_of_month    dayy
2010-02-01  2010-02-13  Saturday
2010-03-01  2010-03-13  Saturday
2010-04-01  2010-04-10  Saturday
2010-05-01  2010-05-08  Saturday
2010-06-01  2010-06-12  Saturday
2010-07-01  2010-07-10  Saturday
Karthik
Hi Karthik,Thanks for the help,If i have any issue i will get back to you..Thanks a lot once again...Riddhi
RIDDHI
Hi Karthik can you please explain that what % in this Query i mean what is the % do in that
RIDDHI
% is modulo operator dear. It gives the remainder value.
Karthik
oho..this i know from my schooling days dear...but i want logic
RIDDHI
Hi riddhi, are you need in solution or logic?Ok logic part DATE_ADD(monthdate, INTERVAL( ( 14 - DAYOFWEEK(monthdate) ) % 7 ) + 7 DAY).It is big part that i workout from orbman pdf.See this link http://oreilly.com/catalog/sqlhks/chapter/ch04.pdfRefer Tip #23 section.
Karthik
Thanks a lot Karthik
RIDDHI