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;
Related
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
I have a table named dept_registration it has a column dept_code so I have to make this field auto-generate but in a specific pattern.
example:- test0001
test0002
test0003
test0004
the "test" should be appended before number automatically after insertion
The column definition could be
dept_code text DEFAULT 'test' || lpad(nextval('myseq')::text, 4, '0')
where myseq is a sequence.
You will get in trouble once the counter reaches 10000...
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
Given a table of the following format in MATLAB:
userid | itemid | keywords
A = [ 3 10 'book'
3 10 'briefcase'
3 10 'boat'
12 20 'windows'
12 20 'picture'
12 35 'love'
4 10 'day'
12 10 'working day'
... ... ... ];
where A is a table of size (58000*3), I want to write the data in a csv file with the following format:
csv.file
itemid keywords
10 book, briefcase, boat, day, working day, ...
20 windows, picture, ...
35 love, ...
where we the list of itemids is stored in Iids = [10,20,35,...]
I would like to avoide using loops for this as you can imagine the matrix is big-sized. Any idea is appreciated.
I wasn't able to think of a solution without loops. But you can optimize your loop by:
using logical indexing
running such loop only M times (if M is the number of unique itemid elements) instead of N times (if N is the number of elements in your table).
The solution I come up with is this.
First of all, create your table
A=table([3;3;3;12;12;12;4;12], [10;10;10;20;20;35;10;10],{'book','briefcase','boat','windows','picture','love','day','working day'}','VariableNames',{'userid','itemid','keywords'});
which looks like
Select the unique values for column itemid (your Iids):
Iids=unique(A.itemid);
which looks like
Create a new, empty, table which will contain the results:
NewTable=table();
And now the minimal loop I've come up with:
for id=Iids'
% select rows with given itemid value
RowsWithGivenId=A(A.itemid==id,:);
% create new row in NewTable with the id and the (joined together) keywords from the selected rows
NewTable=[NewTable; table(id,{strjoin(RowsWithGivenId.keywords,', ')})];
end
Also, append the new column names in NewTable
NewTable.Properties.VariableNames = {'itemid','keywords'};
And now NewTable looks like:
Please note: due to the fact that the keywords in the new table are separated by comma, a csv file is not the format I recommend. By using writetable() as writetable(NewTable,'myfile.csv');
what you'll get is
As instead, by replacing ; instead of a separating comma (in strjoin()), you'll get a nicer format:
there is a table's fields on MSSQL Serrver 2005 as VARCHAR. It contains alphanumeric values like "A,B,C,D ... 1,2,3,...,10,11,12" etc.
When i use below codes;
....
ORDER BY TableFiledName
Ordering result is as follow 11,12,1,2,3 etc.
When i use codes as below,
....
ORDER BY
CASE WHEN ISNUMERIC(TableFiledName) = 0 THEN CAST(TableFiledNameAS INT) ELSE TableFiledName END
I get error message as below;
Msg 8114, Level 16, State 5, Line 1 Error converting data type varchar
to float.
How can get like this ordering result: 1,2,3,4,5,6,7,8,9,10,11,12 etc..
Thanks in advance.
ISNUMERIC returns 1 when the field is numeric.
So your first problem is that it should be ...
CASE WHEN ISNUMERIC(TableFiledName) = 1 THEN
But this alone won't work.
You need to prefix the values with zeroes and take the rightmost
order by
case when ISNUMERIC(FieldName) =1
then right('000000000'+FieldName, 5)
else FieldName
end
Using 5 allows for numbers up to 99999 - if your numbers are higher, increase that number.
This will put the numbers before the letters. If you want the letters before the numbers, then you can add an isnumeric to the sort order - ie:
order by
isnumeric(FieldName),
case...
This won't cope with decimals, but you haven't mentioned them