Petr Fořt Fru-Fru
Petr Fořt Fru-Fru

Reputation: 996

How to search SQL records by date of birth?

I have a SQL-table where I have records of my clients such as first name, last name, date of birth etc. Now I try to find all clients by date of birth (for cron, sending holiday and birthday greetings). Therefore, I need to find all existing clients with a specific date of birth.

For example:

In the SQL table I have a few records with these birth dates.

+==================+
| Date_of_birthday |
====================
|    1981-06-30    |
--------------------
|    1972-06-30    |
--------------------
|    1966-10-07    |
====================

Now I need to return all clients who were born on June 30th.

I tried:

SELECT * FROM `table`
WHERE DATE(Date_of_birthday) = '####-06-30';

But my pattern does not seem to work properly, it doesn't return any lines.

The hashtag character '#' should represent any numeric character, but it doesn't work.

Where is my mistake? Or do I have to write the SQL query differently?

Upvotes: 0

Views: 1401

Answers (3)

Bruno Bukavu Thai
Bruno Bukavu Thai

Reputation: 141

This query should do:

select *
from YourTable
where datepart(day, date_birth) = 30
and datepart(month, date_birth) = 06

Upvotes: 2

ln29st
ln29st

Reputation: 83

SELECT * FROM `table`
WHERE right(Date_of_birthday, 5) = '06-30'

Upvotes: 1

forpas
forpas

Reputation: 164099

With DATE_FORMAT():

SELECT * FROM `table`
WHERE DATE_FORMAT(Date_of_birthday,'%m-%d') = '06-30';

Upvotes: 2

Related Questions