Search for birthdays of the month varchar [duplicate]

1

Personal a customer has a datnasc field of type varchar in his database and now he wants to search for the birthdays of the month.

How can I do this since the values entered in this field are in this pattern:

dd/mm/aaaa = 16/03/2017

    
asked by anonymous 16.03.2017 / 12:57

2 answers

3

Must be transformed into date ( str_to_date ) and then check out the month ( MONTH ) that takes only the month of a certain date, although the correct thing was to write this data in the format date or datetime in the bank, example :

select * from tabela where MONTH(str_to_date(data, '%d/%m/%Y'))=MONTH(CURRENT_DATE())

References:

16.03.2017 / 13:15
0

Use the SUBSTR function to pick up only the users' birthday month. See:

<?php
    $mes = "03"; // mês atual ou escolhido para exibir os aniversariantes

    $sql = "SELECT * FROM USUARIOS WHERE SUBSTR('DATA_NASC', 3, 2) =".$mes;
    $qry = $pdo->prepare($sql);
    $qry->execute();
?>

In this case, given a date 03/16/2017 (for example) the function SUBSTR will pick up the characters from the third position with size 2, that is, position 3 and 4 where the month is.

    
16.03.2017 / 13:20