I have a column named invoice_number varchar(255) in the invoices table.
Here is some sample data:
20220010000000010
20220010000000011
20220010000000012
An invoice_number can have up to 17 digits. Here is the format in which it is generated:
Year(4 digits) + Number of invoice (3 digits) + profile number (10 digits)
At the moment, I have some data in this column as follows:
202200100000022.1
202200100000022.2
202200100000022.3
I would like to delete the decimal point which is the 2nd to the last digit and then add a zero on the 8th position (after 001 according to the sample data above) to handle all of these undesired invoice numbers.
Expected Output:
20220010000000221
20220010000000222
20220010000000223
What would be the best way to do this?
A safe way to do it is using REGEXP_REPLACE.
select invoice_number
, regexp_replace(invoice_number, '^([0-9]{4})([0-9]{3})([0-9]{8})[.]([0-9]+)$', '\1\20\3\4') as new_invoice_number
from (values
('202200100000022.1')
, ('202200100000022.2')
, ('202200100000022.3')
) q(invoice_number)
where invoice_number like '%.%';
invoice_number
new_invoice_number
202200100000022.1
20220010000000221
202200100000022.2
20220010000000222
202200100000022.3
20220010000000223
Test on db<>fiddle here
Related
New to postgres and unsure how to accomplish the following. I have a table as follows:
create table if not exists my_table (
id int GENERATED BY DEFAULT AS IDENTITY primary key,
key int default 0
)
What I am trying to do is take an integer value (my_key) and if it's >= 0 and < 10 then add a leading zero (0) to it and insert it into my_table.key
I have tried to_char(my_key::integer,'09')::integer where my_key = 0 and it doesn't insert 00 within the key column.
Any help would be great.
Leading zeros don't change the value of an integer, so this is a question about formatting numbers.
If you want to display the id column with leading zeros, you could do that like this:
SELECT to_char(id, '00'), key
FROM "table";
The format 00 formats the number as a two-digit string with leasing zeros. If id is greater than 99, the number cannot be formatted like this, and you will get ##.
See the documentation for details about to_char and the available formats.
Telephone_number
111111111
1111143452
000000000
888888888
5554038291
1111392012
9999999999
2324666666
So from the above, I want to have only those records that have the same digit repeated more than 5 times.
The output count should be 5.
You can a regular expression with a repeated back reference.
with test (telephone_number) as
( values ('111111111')
, ('1111143452')
, ('000000000')
, ('888888888')
, ('5554038291')
, ('1111392012')
, ('9999999999')
, ('2324666666')
)
select telephone_number
from test
where telephone_number ~ '(\d)\1{5,}';
This does depend on how the columns are defined, as numeric or as text. If numeric definition then you need to play around in converting to text for using a regular expression. See fiddle.
Is it possible to define a column that auto increments which is a 12 digit number on a schema level?
So the sequence would go 000000000000, 000000000001, ...
You can create a sequence specifying min,max,start values. Then assign that sequence as a default. You commented that your need is "EAN-13, but the first digit is a constant", from this I assume you actually need a 13 digit number beginning with a fixed digit. You can use that fixed digit as the leading value of the sequence. Something like ( assumes that constant first digit is 5):
create sequence barcode_seq
increment 1
minvalue 5000000000000
maxvalue 5999999999999
start 5000000000000;
While sequences tend to be used as table keys that is not a requirement. Use the above sequence as the default value wherever the barcode is assigned. See fiddle.
Are you looking for the serial datatype? That's an auto-incrementing integer. It ranges from 1 to 2147483647, which is a bit less than 12 digits. If you need something bigger, you can switch to bigserial, that goes up to 9223372036854775807.
create table mytable (
id serial,
val text
);
insert into mytable (val) values ('foo'), ('bar');
select * from mytable;
id | val
-: | :--
1 | foo
2 | bar
I am working with PostgreSQL. Now I want to generate a number like as register number. It is a 16 digit code.
format: vvvvvv0000iiiiii
where, v is the village code which is retrieved from table named village_details (field: village_code).
Then next four digit is fixed as zero.
Then the next 6 digits(ie: iiiiii) is customer id (which must increment from 0000001 to iiiiii).
Example:
1212450000111111
1212450000111112
etc..
How it will be generated?
First create a sequence for the customer_id:
CREATE SEQUENCE customer_id;
Then:
SELECT village_code || '0000' || lpad(nextval('customer_id'), 6, '0') FROM village_details;
I am trying to join 2 tables but my problem is that one of the table has 10 digit number and the other one may have 10 or less digit number. For this reason, i am loosing some data so i would like to do is check the length first if the length is less than 10 digit then i want to add leading zeros so i can make it 10 digit number. I want to do this when i am joining this so i am not sure if this is possible. Here is an example if i i have 251458 in the TABLE_WITHOUT_LEADING_ZERO then i want to change it like this: 0000251458. Here is what i have so far:
select ACCT_NUM, H.CODE
FROM TABLE_WITH_LEEDING_ZERO D, TABLE_WITHOUT_LEADING_ZERO H
WHERE substring(D.ACCT_NUM from position('.' in D.ACCT_NUM) + 2) = cast (H.CODE as varchar (10))
thanks
Another alternative:
SELECT TO_CHAR(12345,'fm0000000000');
to_char
------------
0000012345
In Netezza you can use LPAD:
select lpad(s.sample,10,0) as result
from (select 12345 as sample) s
result
-------
0000012345
However it would be more efficient to remove the zeros like in the example below:
select cast(trim(Leading '0' from s.sample) as integer) as result
from (select '0000012345' as sample) s
result
-------
12345