Search for birthdays of the month varchar

Asked

Viewed 454 times

1

Staff a customer has a field datnasc of the kind varchar in his database and now he wants to fetch the birthday of the month.

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

dd/mm/aaaa = 16/03/2017

  • You want to make a select?

  • @Marconi yes I want to make a select showing all the records that in datnasc have the month /03/

  • 1

    Is there an option for you to make a change to your customer’s database structure? Or could this have some impact on any application that’s using it? Well, if you can change the structure without problems, then use the link @Virgilionovic commented, otherwise his answer would also solve, don’t forget to accept it as a solution.

  • Your field is recorded this way: dd/mm/aaaa = 16/03/2017 with all letters and numbers?

2 answers

3

  • @Miguel yes true, the amendment is made, thank you...

  • 1

    You are welcome, I will delete the comment. Good reply

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 16/03/2017(for example) the SUBSTR function will pick up the characters from the third position with size 2, ie position 3 and 4 where the month is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.