pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedDeclare variable from other variable
6+ messages / 4 participants
[nested] [flat]
* Declare variable from other variable
@ 2020-02-05 11:21 Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:28 ` Re: Declare variable from other variable Yasin Sari <yasinsari81@googlemail.com>
2020-02-05 12:29 ` Re: Declare variable from other variable Thomas Kellerer <shammat@gmx.net>
0 siblings, 2 replies; 6+ messages in thread
From: Raul Kaubi @ 2020-02-05 11:21 UTC (permalink / raw)
To: pgsql-general
Hi
PostgreSQL 12.1
How can I declare another variable from another variable.
Basically from oracle, I can just:
> var1 := 'asda'||var2;
In postgres, I have the following example, I would like to use variable j
to add number of months there.
" interval 'j month')::date; "
DO $$
> DECLARE
> v_var integer := 1;
> v_from_date date;
> BEGIN
> for j in 0..v_var LOOP
> v_from_date := (date_trunc('month',current_date) + interval 'j
> month')::date;
> RAISE NOTICE '%', v_from_date;
> END LOOP;
> END;
> $$ LANGUAGE plpgsql;
> ERROR: invalid input syntax for type interval: "j month"
> LINE 1: ...LECT (date_trunc('month',current_date) + interval 'j month')...
Raul
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Declare variable from other variable
2020-02-05 11:21 Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
@ 2020-02-05 12:28 ` Yasin Sari <yasinsari81@googlemail.com>
2020-02-05 12:42 ` Re: Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
1 sibling, 1 reply; 6+ messages in thread
From: Yasin Sari @ 2020-02-05 12:28 UTC (permalink / raw)
To: Raul Kaubi <raulkaubi@gmail.com>; +Cc: pgsql-general
On Wed, Feb 5, 2020 at 2:22 PM Raul Kaubi <raulkaubi@gmail.com> wrote:
>
> DO $$
>> DECLARE
>> v_var integer := 1;
>> v_from_date date;
>> BEGIN
>> for j in 0..v_var LOOP
>> v_from_date := (date_trunc('month',current_date) + interval 'j
>> month')::date;
>> RAISE NOTICE '%', v_from_date;
>> END LOOP;
>> END;
>> $$ LANGUAGE plpgsql;
>
>
>> ERROR: invalid input syntax for type interval: "j month"
>> LINE 1: ...LECT (date_trunc('month',current_date) + interval 'j
>> month')...
>
>
>
>
If you replace red line with this one it will work:
v_from_date := (date_trunc('month',current_date) + interval '1
month'*j)::date;
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Declare variable from other variable
2020-02-05 11:21 Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:28 ` Re: Declare variable from other variable Yasin Sari <yasinsari81@googlemail.com>
@ 2020-02-05 12:42 ` Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:50 ` Re: Declare variable from other variable hubert depesz lubaczewski <depesz@depesz.com>
0 siblings, 1 reply; 6+ messages in thread
From: Raul Kaubi @ 2020-02-05 12:42 UTC (permalink / raw)
To: Yasin Sari <yasinsari81@googlemail.com>; +Cc: pgsql-general
Thanks, it worked!
By the way, what does this "**j"* mean there..? (this does not mean
multiply there?)
And what if, I would like to declare v_to_date also, so that v_to_date is
always + 1 month compared to v_date_from..?
-- This one will work, but can this be done simpler..?
v_to_date := (date_trunc('month',current_date)::date + interval '1 month' +
interval '1 month'*j)::date;
Raul
Kontakt Yasin Sari (<yasinsari81@googlemail.com>) kirjutas kuupäeval K, 5.
veebruar 2020 kell 14:28:
> On Wed, Feb 5, 2020 at 2:22 PM Raul Kaubi <raulkaubi@gmail.com> wrote:
>
>>
>> DO $$
>>> DECLARE
>>> v_var integer := 1;
>>> v_from_date date;
>>> BEGIN
>>> for j in 0..v_var LOOP
>>> v_from_date := (date_trunc('month',current_date) + interval 'j
>>> month')::date;
>>> RAISE NOTICE '%', v_from_date;
>>> END LOOP;
>>> END;
>>> $$ LANGUAGE plpgsql;
>>
>>
>>> ERROR: invalid input syntax for type interval: "j month"
>>> LINE 1: ...LECT (date_trunc('month',current_date) + interval 'j
>>> month')...
>>
>>
>>
>>
> If you replace red line with this one it will work:
>
> v_from_date := (date_trunc('month',current_date) + interval '1
> month'*j)::date;
>
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Declare variable from other variable
2020-02-05 11:21 Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:28 ` Re: Declare variable from other variable Yasin Sari <yasinsari81@googlemail.com>
2020-02-05 12:42 ` Re: Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
@ 2020-02-05 12:50 ` hubert depesz lubaczewski <depesz@depesz.com>
2020-02-05 14:02 ` Re: Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: hubert depesz lubaczewski @ 2020-02-05 12:50 UTC (permalink / raw)
To: Raul Kaubi <raulkaubi@gmail.com>; +Cc: Yasin Sari <yasinsari81@googlemail.com>; pgsql-general
On Wed, Feb 05, 2020 at 02:42:42PM +0200, Raul Kaubi wrote:
> Thanks, it worked!
>
> By the way, what does this "**j"* mean there..? (this does not mean
> multiply there?)
it's normal multiplication.
Your "j" variable is integer.
So, '1 month'::interval * j is some number of months.
> And what if, I would like to declare v_to_date also, so that v_to_date is
> always + 1 month compared to v_date_from..?
v_to_date := v_from_date + '1 month'::interval; ?
Best regards,
depesz
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Declare variable from other variable
2020-02-05 11:21 Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:28 ` Re: Declare variable from other variable Yasin Sari <yasinsari81@googlemail.com>
2020-02-05 12:42 ` Re: Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:50 ` Re: Declare variable from other variable hubert depesz lubaczewski <depesz@depesz.com>
@ 2020-02-05 14:02 ` Raul Kaubi <raulkaubi@gmail.com>
0 siblings, 0 replies; 6+ messages in thread
From: Raul Kaubi @ 2020-02-05 14:02 UTC (permalink / raw)
To: depesz@depesz.com; +Cc: Yasin Sari <yasinsari81@googlemail.com>; pgsql-general
Makes sense yeah.
Thanks for both of your help.
Raul
Kontakt hubert depesz lubaczewski (<depesz@depesz.com>) kirjutas kuupäeval
K, 5. veebruar 2020 kell 14:50:
> On Wed, Feb 05, 2020 at 02:42:42PM +0200, Raul Kaubi wrote:
> > Thanks, it worked!
> >
> > By the way, what does this "**j"* mean there..? (this does not mean
> > multiply there?)
>
> it's normal multiplication.
>
> Your "j" variable is integer.
>
> So, '1 month'::interval * j is some number of months.
> > And what if, I would like to declare v_to_date also, so that v_to_date is
> > always + 1 month compared to v_date_from..?
>
> v_to_date := v_from_date + '1 month'::interval; ?
>
> Best regards,
>
> depesz
>
>
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Declare variable from other variable
2020-02-05 11:21 Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
@ 2020-02-05 12:29 ` Thomas Kellerer <shammat@gmx.net>
1 sibling, 0 replies; 6+ messages in thread
From: Thomas Kellerer @ 2020-02-05 12:29 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Raul Kaubi schrieb am 05.02.2020 um 12:21:
> How can I declare another variable from another variable.
> Basically from oracle, I can just:
>
> var1 := 'asda'||var2;
>
> In postgres, I have the following example, I would like to use variable j to add number of months there.
>
> " interval 'j month')::date; "
>
>
> DO $$
> DECLARE
> v_var integer := 1;
> v_from_date date;
> BEGIN
> for j in 0..v_var LOOP
> v_from_date := (date_trunc('month',current_date) + interval 'j month')::date;
> RAISE NOTICE '%', v_from_date;
> END LOOP;
> END;
> $$ LANGUAGE plpgsql;
The easiest way is to use make_interval()
v_from_date := (date_trunc('month',current_date) + make_interval(months => j))::date;
But it sounds as if generate_series() is what you are really looking for.
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2020-02-05 14:02 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-02-05 11:21 Declare variable from other variable Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:28 ` Yasin Sari <yasinsari81@googlemail.com>
2020-02-05 12:42 ` Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:50 ` hubert depesz lubaczewski <depesz@depesz.com>
2020-02-05 14:02 ` Raul Kaubi <raulkaubi@gmail.com>
2020-02-05 12:29 ` Thomas Kellerer <shammat@gmx.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox