How to make a query that only refers to the DAY of a date?

-1

I have a MYSQL database and I need a SQL that looks at the DAY OF PAYMENT in the DIA field (this field will only have the DAY of the month that the client chose to make the payment, this field is of type INT) and show me some less than 5 days before the payday. Of course this DAY field will be based on the day of the current date.

I have this code:

Select * from conta where DAYOFMONTH(CURDATE()) - 5 = dia;

Only that it does not work, because it only shows the records that are missing 5 days to the due date, that is, if 4 days is missing, 03 days, 02 days, 01 day it does not show anything. I need something that always shows some result when less than 5 days to maturity.

    
asked by anonymous 29.03.2016 / 21:26

3 answers

4
Select * from tabela where DAYOFMONTH(CURDATE())-5 = colunaDia;
    
29.03.2016 / 22:04
1

Beauty?

Dude, I do not know how your basic structure is, but just using the pay day gets a lot more complicated to validate. Look at a situation, today is day 29, if I consider everyone who has less than 5 days to win would have to catch everyone from today until April 3. So if your client has marked that day 1 is his payday you will have to consider that "29" - "1" will meet your 5 day rule because you are going through your doubt that you have the option to work only with the current date and a whole day field.

However ...

If you want to track the maturities, it would be interesting to have a table with all the postings with their respective maturities forward.

Ex:

  

Customer wants to pay something, today on 3/29, in 3x and his payday will be every 10th day.

logo

  

I have a table where I am going to post three movements for future collection with maturity of: 10/04/2016 , 10/05/2016 and 10/06 / 2016 .

So it's easier and simpler to make a difference of the dates and check if the number of days is less than the 5 you want.

Anything explains a little more how your table structure is, without knowing the bank is a little more complicated to help.

If my suggestion was helpful, flag it there! Thanks!

    
30.03.2016 / 01:04
0

You can use DATEDIFF to do this.

In the function you have to use the current date in the first parameter in the second you add the total of days that you check the current date ie 5 . DATEADD (day, 5, getdate ()) with days added just do DATEDIFF

DATEDIFF(day, GETDATE() , DATEADD(day,5, getdate()))

declare @Usuario table
(
  nome varchar(20),
  diapagamento int
)


insert into @Usuario values
( 'zé', 5),
( 'fulano', 15),
( 'manoel', 25),
( 'joao', 30),
( 'carlos', 5),
( 'zpauloé', 2)


select *  from @Usuario
where diapagamento <= DATEDIFF(day, GETDATE() , DATEADD(day,5, getdate()))
  

diapagamento

29.03.2016 / 23:47