show table data between two chosen dates (dates are in varchar)

Asked

Viewed 65 times

0

SELECT * FROM minhaTabela WHERE status=2
AND nomePessoa='Jose Silva' AND STR_TO_DATE(data_enviado, '%d/%m/%Y')
BETWEEN STR_TO_DATE('01/05/2015', '%d/%m/%Y')
AND STR_TO_DATE('31/06/2015', '%d/%m/%Y')

Is there any way to do an if/Else? I have other tables that date does not follow this pattern of day/month/year (this is already dealt with up there). some tables, the column data_enviado, is as year-day, some others as year/month/day, others like day-month-year; I could do the processing in my php code by checking the tables name, but if there is an easier way to do this by sql...

  • The ideal was to create a new column of the type date with the values you already have in the database, so ends this confusion.

1 answer

0

Jerry, try:

SELECT * 
FROM 
     minhaTabela
WHERE 
      status=2 AND 
      nomePessoa='Jose Silva' AND 
      DATE_FORMAT(str_to_date(data_enviado, '%d/%m/%Y'), '%Y-%m-%d')
      BETWEEN DATE_FORMAT(str_to_date('01/05/2015', '%d/%m/%Y'), '%Y-%m-%d') AND
      DATE_FORMAT(str_to_date('31/06/2015', '%d/%m/%Y'), '%Y-%m-%d');

Browser other questions tagged

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