Heisenberg
Heisenberg

Reputation: 5299

How to assign date to given variable correctly

I'd like to get reference date as previous saturday when the query is executed before wednesday,after that reference date is executed day.and I want to reuse this reference date as reference_date . to achieve this, I write following queries.But when I try to assign date to reference_date it returned errors.

Are there any way to achieve this ? Thanks

select into reference_date 
    case
        when date_part(dw, current_date) <= 3 then (
            current_date - (
                date_part(dw, current_date) :: integer + 1
            )
        )
        else current_date
    end;
    
select reference_date;

Upvotes: 0

Views: 25

Answers (1)

Edouard
Edouard

Reputation: 7065

Try this :

select 
    case
        when date_part('dow', current_date) <= 3 
        then current_date - (date_part('dow', current_date) :: integer + 1)
        else current_date
    end
into reference_date ;

Upvotes: 1

Related Questions