MySQL Week function

MySQL Week function

am 24.01.2005 19:50:19 von Bas Varkevisser

Hi,

I have a table with birthdates and only want the persons that have their
birthday in this week.

I use SELECT * FROM persons WHERE week(birthdate) = week(NOW());

That returns birthdays from last sunday until next tuesday(!)

Does anyone know why it returns too many days?

Thanks,
Bas.

Re: MySQL Week function

am 25.01.2005 16:32:14 von Steve

> I have a table with birthdates and only want the persons that have
their
> birthday in this week.

You need to ask if the person's birthday THIS YEAR falls in this week.

You are actually asking if the person's birthday in the year of their
birth fell the same number of weeks after the start of the year as
today's date does for this year. This is not a useful question for the
purpose...

Substitute the current year into the person's birthdate before asking
the question:

SELECT *
FROM persons
WHERE WEEK(
CONCAT( YEAR( NOW() ),
CONCAT( "-",
CONCAT( MONTH( Birthdate ),
CONCAT( "-", DAYOFMONTH( Birthdate )
)
)
)
)
) = WEEK( NOW() )
FROM mytable

---
Steve