How could I do to pick up every day of the current month?

Asked

Viewed 37 times

1

First of all, good morning guys, I would like a little help in filtering out every day of the current month, where I could get from the first to the 30th or 31st of the current month, where I wouldn’t define what month I want but what’s in the machine.

How could I do a search without a fixed month?

base_cliente_comprou (ID) AS (
    SELECT * FROM tb_pedido 
    INNER JOIN base ON cliente = ped_cli_id
    WHERE ped_data BETWEEN '2020-07-01 00:00:00' AND '2020-07-31 23:59:59' AND ped_cli_id NOT IN (cliente)

)

Thanks in advance.

  • only the month, or month and year?

  • For example: ped_data >= date_trunc('month', CURRENT_DATE) AND ped_data < (date_trunc('month', CURRENT_DATE) + interval '1 month')

  • Hello guys, thank you so much for the answer, I tested the two ways you sent me and it worked super well, thank you, I’m already applying in my code. Hug...

  • Claudio Lopes, I needed the year and I forgot to ask too, but Marcos' answer downstairs ends up getting the year too, well thank you very much anyway ;)

  • 1

    Just for the current month can simplify to: data_trunc('month', CURRENT_DATE) = date_trunc('month', ped_data)

1 answer

0


Good morning,

From what I understand, you want all the records for a given month. If that’s the case, use something like this:

base_cliente_comprou (ID) AS (
    SELECT * FROM tb_pedido 
    INNER JOIN base ON cliente = ped_cli_id
    WHERE 
    extract('month' from ped_data) = extract('month' from current_date)
    and extract ('year' from ped_data) = extract('year' from current_date)
)
  • 1

    To Marcos, thank you, I tested the form you sent and it worked very well too, thank you very much for the help, strong hug...

  • Please, just mark the answer as accepted, with this, other users in the same situation will be helped as well. Hugs

Browser other questions tagged

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