Y
Y
Yaros752021-01-19 17:01:57
PostgreSQL
Yaros75, 2021-01-19 17:01:57

Cant when converting text to number, Why are digits lost?

The table has a column with identifiers of the form YYMM.DD

## 2101.1601#
2101.1602#
2101.1701#
i.e. is the date plus a two-digit number. I need to create a new id for a specific date.

Regularly I select the last 3 digits from identifiers for a specific date, for example, 01/16/2021

SELECT REGEXP_MATCHES(trade number, '\d\d\d(?=\D*$)', 'g')
FROM db.Trade
WHERE deal number ~ '2101\ .sixteen';
I get a set: (by the way, what do these brackets mean when outputting?)

{601}
{602}
now I need to change their type to integer.

Tried to_number( val, '999' ); For some reason, it returns 60 instead of 601, one bit disappears somewhere.

If I write manually to_number( '601', '999' ); the result is correct,

if I write manually with brackets as in the selection cuts to_number( '{601}', '999' );

then again I get the same cant 60 instead of 601. What's the matter?

Here is the

CREATE OR REPLACE FUNCTION db.extract_nom(integer)
RETURNS text
LANGUAGE plpgsql
AS $function$
DECLARE
trade_id ALIAS FOR $1;
output integer;
regtext;
row_data text;
BEGIN

SELECT to_char( Orderdate, 'YYMM\.DD') INTO reg FROM db.Deals WHERE id=id_Deals;

-- Iterating over query results.
FOR row_data IN SELECT REGEXP_MATCHES(nomTrade, '\d\d\d(?=\D*$)', 'g') FROM "db"."
WHERE "number of trade" ~ reg ORDER BY "number of trade" DESC LOOP

output := to_number(row_data[1], '000');
END LOOP;

RETURN output;
END;
$function$

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-01-19
@freeExec

The curly braces mean arrays.

If I write manually with brackets, as in the cut of the selection to_number( '{601}', '999' );

Everything should be obvious right away, your comparison goes over:
{60
999
as a result: 60
If you already test, then
SELECT to_number( (ARRAY['601'])[1], '999' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question