public inbox for [email protected]  
help / color / mirror / Atom feed
Postgres View DDL
5+ messages / 3 participants
[nested] [flat]

* Postgres View DDL
@ 2024-10-16 21:33  Sam Stearns <[email protected]>
  0 siblings, 1 reply; 5+ messages in thread

From: Sam Stearns @ 2024-10-16 21:33 UTC (permalink / raw)
  To: pgsql-sql <[email protected]>

Howdy,

I have an Oracle view that's been converted for Postgres.  This block of
code in the Oracle view DDL:

TO_NUMBER (NVL (REGEXP_REPLACE (broker_mc, '[^0-9]+', ''), 0)),
TO_NUMBER (NVL (REGEXP_REPLACE (carrier_mc, '[^0-9]+', ''), 0)),
TO_NUMBER (NVL (REGEXP_REPLACE (freight_forwarder_mc, '[^0-9]+', ''), 0)),

has been converted for Postgres as:

(coalesce(REGEXP_REPLACE(broker_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
(coalesce(REGEXP_REPLACE(carrier_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
(coalesce(REGEXP_REPLACE(freight_forwarder_mc, '[^0-9]+', '', 'g'),
0))::numeric ,

which is throwing the following error:

ERROR:  COALESCE types text and integer cannot be matched
LINE 43: ...ce(REGEXP_REPLACE(broker_mc, '[^0-9]+', '', 'g'), 0))::numer...

                         ^
I have been looking through:

https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL
https://www.postgresql.org/docs/current/typeconv-union-case.html

but I'm not seeing a way to resolve it.  Would anyone be able to advise how
to correct this for Postgres, please?

Thanks,

Sam

-- 

*Samuel Stearns*
Lead Database Administrator
*c:* 971 762 6879 | *o:* 503 672 5115 | DAT.com
[image: DAT]
<https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link;


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Postgres View DDL
@ 2024-10-16 21:42  Sam Stearns <[email protected]>
  parent: Sam Stearns <[email protected]>
  0 siblings, 2 replies; 5+ messages in thread

From: Sam Stearns @ 2024-10-16 21:42 UTC (permalink / raw)
  To: pgsql-sql <[email protected]>

Tried changing to this:

    (coalesce(REGEXP_REPLACE(broker_mc::numeric, '[^0-9]+', '', 'g'), 0)),
    (coalesce(REGEXP_REPLACE(carrier_mc::numeric, '[^0-9]+', '', 'g'), 0)),
    (coalesce(REGEXP_REPLACE(freight_forwarder_mc::numeric, '[^0-9]+', '',
'g'), 0)),

but that throws this error:

ERROR:  function regexp_replace(numeric, unknown, unknown, unknown) does
not exist
LINE 46:     (coalesce(REGEXP_REPLACE(broker_mc::numeric, '[^0-9]+', ...
                                    ^
HINT:  No function matches the given name and argument types. You might
need to add explicit type casts.

Sam

On Wed, Oct 16, 2024 at 2:33 PM Sam Stearns <[email protected]> wrote:

> Howdy,
>
> I have an Oracle view that's been converted for Postgres.  This block of
> code in the Oracle view DDL:
>
> TO_NUMBER (NVL (REGEXP_REPLACE (broker_mc, '[^0-9]+', ''), 0)),
> TO_NUMBER (NVL (REGEXP_REPLACE (carrier_mc, '[^0-9]+', ''), 0)),
> TO_NUMBER (NVL (REGEXP_REPLACE (freight_forwarder_mc, '[^0-9]+', ''), 0)),
>
> has been converted for Postgres as:
>
> (coalesce(REGEXP_REPLACE(broker_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
> (coalesce(REGEXP_REPLACE(carrier_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
> (coalesce(REGEXP_REPLACE(freight_forwarder_mc, '[^0-9]+', '', 'g'),
> 0))::numeric ,
>
> which is throwing the following error:
>
> ERROR:  COALESCE types text and integer cannot be matched
> LINE 43: ...ce(REGEXP_REPLACE(broker_mc, '[^0-9]+', '', 'g'), 0))::numer...
>
>                            ^
> I have been looking through:
>
>
> https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL
> https://www.postgresql.org/docs/current/typeconv-union-case.html
>
> but I'm not seeing a way to resolve it.  Would anyone be able to advise
> how to correct this for Postgres, please?
>
> Thanks,
>
> Sam
>
> --
>
> *Samuel Stearns*
> Lead Database Administrator
> *c:* 971 762 6879 | *o:* 503 672 5115 | DAT.com
> [image: DAT]
> <https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link;
>


-- 

*Samuel Stearns*
Lead Database Administrator
*c:* 971 762 6879 | *o:* 503 672 5115 | DAT.com
[image: DAT]
<https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link;


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Postgres View DDL
@ 2024-10-16 21:47  David G. Johnston <[email protected]>
  parent: Sam Stearns <[email protected]>
  1 sibling, 0 replies; 5+ messages in thread

From: David G. Johnston @ 2024-10-16 21:47 UTC (permalink / raw)
  To: Sam Stearns <[email protected]>; +Cc: pgsql-sql <[email protected]>

On Wednesday, October 16, 2024, Sam Stearns <[email protected]> wrote:

> Tried changing to this:
>     (coalesce(REGEXP_REPLACE(broker_mc::numeric, '[^0-9]+', '', 'g'), 0)),
>

Both parts of the coalesce need to be integer.  Therefore:

regexp_replace(…)::integer

In short, the output of the text manipulation better be something that can
be cast to integer if you want coalesce to produce an integer.

David J.


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Postgres View DDL
@ 2024-10-16 22:04  William Alves Da Silva <[email protected]>
  parent: Sam Stearns <[email protected]>
  1 sibling, 1 reply; 5+ messages in thread

From: William Alves Da Silva @ 2024-10-16 22:04 UTC (permalink / raw)
  To: Sam Stearns <[email protected]>; +Cc: pgsql-sql <[email protected]>

Hello Sam.

I think you want this:

SELECT COALESCE(regexp_replace('abc12345'::TEXT,  '[^0-9]+', '', 'g')::NUMERIC, 0);

The coalesce need the same type from origin field, or you cast the result from regex to interger/numeric etc, or you use cast ‘0’ instead of 0;

Regards,
William Alves.

> On 16 Oct 2024, at 18:42, Sam Stearns <[email protected]> wrote:
> 
> Tried changing to this:
> 
>     (coalesce(REGEXP_REPLACE(broker_mc::numeric, '[^0-9]+', '', 'g'), 0)),
>     (coalesce(REGEXP_REPLACE(carrier_mc::numeric, '[^0-9]+', '', 'g'), 0)),
>     (coalesce(REGEXP_REPLACE(freight_forwarder_mc::numeric, '[^0-9]+', '', 'g'), 0)),
> 
> but that throws this error:
> 
> ERROR:  function regexp_replace(numeric, unknown, unknown, unknown) does not exist
> LINE 46:     (coalesce(REGEXP_REPLACE(broker_mc::numeric, '[^0-9]+', ...
>                                     ^
> HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
> 
> Sam
> 
> On Wed, Oct 16, 2024 at 2:33 PM Sam Stearns <[email protected] <mailto:[email protected]>> wrote:
>> Howdy,
>> 
>> I have an Oracle view that's been converted for Postgres.  This block of code in the Oracle view DDL:
>> 
>> TO_NUMBER (NVL (REGEXP_REPLACE (broker_mc, '[^0-9]+', ''), 0)),
>> TO_NUMBER (NVL (REGEXP_REPLACE (carrier_mc, '[^0-9]+', ''), 0)),
>> TO_NUMBER (NVL (REGEXP_REPLACE (freight_forwarder_mc, '[^0-9]+', ''), 0)),
>> 
>> has been converted for Postgres as:
>> 
>> (coalesce(REGEXP_REPLACE(broker_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
>> (coalesce(REGEXP_REPLACE(carrier_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
>> (coalesce(REGEXP_REPLACE(freight_forwarder_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
>> 
>> which is throwing the following error:
>> 
>> ERROR:  COALESCE types text and integer cannot be matched
>> LINE 43: ...ce(REGEXP_REPLACE(broker_mc, '[^0-9]+', '', 'g'), 0))::numer...
>>                                                                                                      ^
>> I have been looking through:
>> 
>> https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL
>> https://www.postgresql.org/docs/current/typeconv-union-case.html
>> 
>> but I'm not seeing a way to resolve it.  Would anyone be able to advise how to correct this for Postgres, please?
>> 
>> Thanks,
>> 
>> Sam
>> 
>> --
>> Samuel Stearns
>> Lead Database Administrator
>> c: 971 762 6879 | o: 503 672 5115 | DAT.com
>> 
>>  <https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link;
> 
> --
> Samuel Stearns
> Lead Database Administrator
> c: 971 762 6879 | o: 503 672 5115 | DAT.com
> 
>  <https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link;


^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Postgres View DDL
@ 2024-10-16 23:07  Sam Stearns <[email protected]>
  parent: William Alves Da Silva <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Sam Stearns @ 2024-10-16 23:07 UTC (permalink / raw)
  To: William Alves Da Silva <[email protected]>; +Cc: pgsql-sql <[email protected]>

Just adopting William's advice and changing to this:

(coalesce(REGEXP_REPLACE(broker_mc::text, '[^0-9]+', '', 'g')::numeric, 0)),
(coalesce(REGEXP_REPLACE(carrier_mc::text, '[^0-9]+', '', 'g')::numeric,
0)),
(coalesce(REGEXP_REPLACE(freight_forwarder_mc::text, '[^0-9]+', '',
'g')::numeric, 0)),

has resolved the problem.  Thank you, William and David!  Learning a lot
here.  Appreciate all the help.

Sam


On Wed, Oct 16, 2024 at 3:04 PM William Alves Da Silva <
[email protected]> wrote:

> Hello Sam.
>
> I think you want this:
>
> SELECT COALESCE(regexp_replace('abc12345'::TEXT,  '[^0-9]+', '',
> 'g')::NUMERIC, 0);
>
> The coalesce need the same type from origin field, or you cast the result
> from regex to interger/numeric etc, or you use cast ‘0’ instead of 0;
>
> Regards,
> William Alves.
>
> On 16 Oct 2024, at 18:42, Sam Stearns <[email protected]> wrote:
>
> Tried changing to this:
>
>     (coalesce(REGEXP_REPLACE(broker_mc::numeric, '[^0-9]+', '', 'g'), 0)),
>     (coalesce(REGEXP_REPLACE(carrier_mc::numeric, '[^0-9]+', '', 'g'), 0)),
>     (coalesce(REGEXP_REPLACE(freight_forwarder_mc::numeric, '[^0-9]+', '',
> 'g'), 0)),
>
> but that throws this error:
>
> ERROR:  function regexp_replace(numeric, unknown, unknown, unknown) does
> not exist
> LINE 46:     (coalesce(REGEXP_REPLACE(broker_mc::numeric, '[^0-9]+', ...
>                                     ^
> HINT:  No function matches the given name and argument types. You might
> need to add explicit type casts.
>
> Sam
>
> On Wed, Oct 16, 2024 at 2:33 PM Sam Stearns <[email protected]> wrote:
>
>> Howdy,
>>
>> I have an Oracle view that's been converted for Postgres.  This block of
>> code in the Oracle view DDL:
>>
>> TO_NUMBER (NVL (REGEXP_REPLACE (broker_mc, '[^0-9]+', ''), 0)),
>> TO_NUMBER (NVL (REGEXP_REPLACE (carrier_mc, '[^0-9]+', ''), 0)),
>> TO_NUMBER (NVL (REGEXP_REPLACE (freight_forwarder_mc, '[^0-9]+', ''), 0)),
>>
>> has been converted for Postgres as:
>>
>> (coalesce(REGEXP_REPLACE(broker_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
>> (coalesce(REGEXP_REPLACE(carrier_mc, '[^0-9]+', '', 'g'), 0))::numeric ,
>> (coalesce(REGEXP_REPLACE(freight_forwarder_mc, '[^0-9]+', '', 'g'),
>> 0))::numeric ,
>>
>> which is throwing the following error:
>>
>> ERROR:  COALESCE types text and integer cannot be matched
>> LINE 43: ...ce(REGEXP_REPLACE(broker_mc, '[^0-9]+', '', 'g'),
>> 0))::numer...
>>
>>                            ^
>> I have been looking through:
>>
>>
>> https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL
>> https://www.postgresql.org/docs/current/typeconv-union-case.html
>>
>> but I'm not seeing a way to resolve it.  Would anyone be able to advise
>> how to correct this for Postgres, please?
>>
>> Thanks,
>>
>> Sam
>>
>> --
>>
>> *Samuel Stearns*
>> Lead Database Administrator
>> *c:* 971 762 6879 | *o:* 503 672 5115 | DAT.com
>> [image: DAT]
>> <https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link;
>>
>
>
> --
>
> *Samuel Stearns*
> Lead Database Administrator
> *c:* 971 762 6879 | *o:* 503 672 5115 | DAT.com
> [image: DAT]
> <https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link;
>
>
>

-- 

*Samuel Stearns*
Lead Database Administrator
*c:* 971 762 6879 | *o:* 503 672 5115 | DAT.com
[image: DAT]
<https://www.dat.com/?utm_medium=email&utm_source=DAT_email_signature_link;


^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2024-10-16 23:07 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-10-16 21:33 Postgres View DDL Sam Stearns <[email protected]>
2024-10-16 21:42 ` Sam Stearns <[email protected]>
2024-10-16 21:47   ` David G. Johnston <[email protected]>
2024-10-16 22:04   ` William Alves Da Silva <[email protected]>
2024-10-16 23:07     ` Sam Stearns <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox