PostgreSQL - return n-sized varchar from function - postgresql

As I found in documentation:
Parenthesized type modifiers (e.g., the precision field for type
numeric) are discarded by CREATE FUNCTION
Are there any alternatives to return varchar(N) type from plpgsql function?
question update:
On picture you can see that Name column recognised as varchar(128), however Number column is recognised as nonsized varchar
f_concat function returns: cast(res as varchar(255));

You can preserve the type modifier for a function result by creating a domain. Postgres will use the underlying varchar(N) type when sending column descriptions to your client:

Related

error on altering table column type in postgresql - bigint to character varying

Below is the query:
alter table customer_master alter column pk_customer_id type character varying(20);
I got the following error message:
ERROR: operator does not exist: character varying > integer HINT: No
operator matches the given name and argument type(s). You might need
to add explicit type casts.
CREATE TABLE customer_master(
pk_customer_id bigint NOT NULL,
created_customer_name character varying(200) DEFAULT NULL::character varying,
fk_deployment_id bigint NOT NULL,
is_active integer
)
here pk_customer_id is the primary key column for this table.
This should work
USING as per documentation:
The optional USING clause specifies how to compute the new column
value from the old; if omitted, the default conversion is the same as
an assignment cast from old data type to new. A USING clause must be
provided if there is no implicit or assignment cast from old to new
type
ALTER TABLE customer_master
ALTER COLUMN pk_customer_id type CHARACTER VARYING(20)
USING pk_customer_id::VARCHAR; -- add using keyword

postgresql exception catching or error handling in postgresql

In below code i have created sample table and written store procedure for exception handling ,the problem is if i insert integer values into columns name and email it is executing .if i pass integer values for name and email columns it should throw exception saying that your passing data types is wrong for name and email columns.
Can any one help me.
CREATE TABLE people
(
id integer NOT NULL,
name text,
email text,
CONSTRAINT people_pkey PRIMARY KEY (id)
)
CREATE OR REPLACE FUNCTION test() RETURNS integer AS'
BEGIN
BEGIN
INSERT INTO people(id,name,email) values(1,5,6);
EXCEPTION
WHEN OTHERS THEN RETURN -1;
END;
RETURN 1;
END'LANGUAGE plpgsql;
select * from test()
select * from people
This is the normal behavior, and it's not related to exception or error handling.
Assigning a numeric value to a text field in an SQL query works seamlessly, because PostgreSQL applies an implicit cast to the numeric literal (this also works with just about any datatype, since they all have a text representation through their I/O conversion routine).
This is tangentially mentioned in the doc for CREATE CAST:
It is normally not necessary to create casts between user-defined
types and the standard string types (text, varchar, and char(n), as
well as user-defined types that are defined to be in the string
category). PostgreSQL provides automatic I/O conversion casts for
that. The automatic casts to string types are treated as assignment
casts, while the automatic casts from string types are explicit-only.

PostgreSQL 9.3: Migrate text column to macaddr type

I have a table with a text column representing a MAC address, but I want to use the macaddr type instead. So, I tried:
alter table mytable alter column mac_column type macaddr;
And got this error
ERROR: column "mac_column" cannot be cast automatically to type macaddr
HINT: Specify a USING expression to perform the conversion.
But I don't know what to use as USING expression:
alter table mytable alter column mac_column type macaddr using(????????)
What should I use as USING expression?
Many thanks in advance
You can’t simply change the data type because data is already there in the column. Since the data is of type String PostgreSQL can't expect it as macaddr though you entered valid String representation of the macaddr. So now, as PostgreSQL suggested you can use the ‘USING’ expression to cast your data into macaddr.
ALTER TABLE mytable ALTER COLUMN mac_column TYPE macaddr USING(mac_column::macaddr)

Postgres Alter table to convert column type from char to bigint [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to change column datatype from character to numeric in postgresql 8.4
If I have a field of type varchar (and all the values are null or string representations of numbers) how do I use alter table to convert this column type to bigint?
To convert simply by parsing the string (casting):
alter table the_table alter column the_column type bigint using the_column::bigint
In fact, you can use any expression in terms of the_column instead of the_column::bigint to customise the conversion.
Note this will rewrite the table, locking out even readers until it's done.
You could create a temporary column of type bigint, and then execute SQL like
UPDATE my_table SET bigint_column=varchar_column::bigint;
Then drop your varchar_column and rename bigint_column. This is kinda roundabout, but will not require a custom cast in postgres.
How to convert a string column type to numeric or bigint in postgresql
Design your own custom cast from string to bigint. Something like this:
CREATE OR REPLACE FUNCTION convert_to_bigint(v_input text)
RETURNS BIGINT AS $$
DECLARE v_bigint_value BIGINT DEFAULT NULL;
BEGIN
BEGIN
v_bigint_value := v_input::BIGINT;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'Invalid bigint value: "%". Returning something else.', v_input;
RETURN 0;
END;
RETURN v_bigint_value;
END;
Then create a new table fixed_table_with_bigint with the same parameters as the old table except change the string column into the bigint column.
Then insert all the rows from the previous table (using the custom cast convert_to_integer ) into the new table:
insert into fixed_table_with_bigint
select mycolumn1,
convert_to_bigint(your_string_bigint_column),
mycolumn3
from incorrect_table
You may have to modify convert_to_bigint in order to handle strings which are not numbers, blankstrings, nulls, control characters and other Weirdness.
Then delete the first table and rename the 2nd table as the first table.

How to change column datatype from character to numeric in PostgreSQL 8.4

I am using following query:
ALTER TABLE presales ALTER COLUMN code TYPE numeric(10,0);
to change the datatype of a column from character(20) to numeric(10,0) but I am getting the error:
column "code" cannot be cast to type numeric
You can try using USING:
The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.
So this might work (depending on your data):
alter table presales alter column code type numeric(10,0) using code::numeric;
-- Or if you prefer standard casting...
alter table presales alter column code type numeric(10,0) using cast(code as numeric);
This will fail if you have anything in code that cannot be cast to numeric; if the USING fails, you'll have to clean up the non-numeric data by hand before changing the column type.
If your VARCHAR column contains empty strings (which are not the same as NULL for PostgreSQL as you might recall) you will have to use something in the line of the following to set a default:
ALTER TABLE presales ALTER COLUMN code TYPE NUMERIC(10,0)
USING COALESCE(NULLIF(code, '')::NUMERIC, 0);
(found with the help of this answer)
Step 1: Add new column with integer or numeric as per your requirement
Step 2: Populate data from varchar column to numeric column
Step 3: drop varchar column
Step 4: change new numeric column name as per old varchar column