PostgreSQL Text column to integer column - postgresql

I have to convert one text column to integer column.
Showed code work when I have text (all numbers) in fields but now I find some empty strings '' where query stops.
ALTER TABLE mytable
ALTER COLUMN mycolumn TYPE integer USING (TRIM(mycolumn)::integer);
Can here be done so that empty strings be converted to integer 0 so this query will pass?
How to do this?

You can update your values before altering table:
UPDATE mytable SET mycolumn = '0' WHERE mycolumn = '';

Related

Setting an empty column as a timestamp with time zone when creating a table in Postgres

I am trying to create a table that has an column called decay_date that will have empty values (for the time being) but be formatted as a timestamp with time zone data type. If I simply do this:
CREATE TABLE my_schema.final_summed_table AS
SELECT
'Unstudied' AS table_name,
r.region,
r.state,
r.co_fips,
c.co_name AS county,
'TIER 0' AS tiermetric_lfd,
'UNASSESSED' AS val_combine_lfd,
'Unmapped' AS mod_unmod_lfd,
'' AS det_approx_lfd,
'' AS decay_date_lfd TYPE TIMESTAMP WITH TIME ZONE NULL USING decay_date_lfd::TIMESTAMP
FROM my_schema.unmapped r, cnms.counties c
WHERE r.co_fips = c.co_fips
I get an error: Syntax error near 'TYPE' This table will be UNIONED later with another table that has the same decay_date column with the data type timestamp with time zone. How do I set the timestamp datatype for my decay_date column while creating my table?
'' isn't a valid value for a timestamp to begin with. And you can't use USING like that in a column alias. That's only allowed (and needed) when you ALTER a table and change the type of a column.
Just select a null value and cast that to the desired type:
null::timestamptz AS decay_date_lfd
That is bad syntax. You'd have to cast the literal to the desired type.
So use a NULL value:
CREATE TABLE my_schema.final_summed_table AS
SELECT
CAST ('Unstudied' AS text) AS table_name,
r.region,
r.state,
r.co_fips,
c.co_name AS county,
CAST ('TIER 0' AS text) AS tiermetric_lfd,
CAST ('UNASSESSED' AS text) AS val_combine_lfd,
CAST ('Unmapped' AS text) AS mod_unmod_lfd,
CAST (NULL AS text) AS det_approx_lfd,
CAST (decay_date_lfd AS timestamp with time zone) AS decay_date_lfd
FROM my_schema.unmapped r, cnms.counties c
WHERE r.co_fips = c.co_fips;
I'd avoid CREATE TABLE ... AS and use CREATE TABLE and INSERT INTO ... SELECT ... instead.

COALESCE types character varying and numeric cannot be matched

I have a table named table1 with columns Gender varchar(10) and Team numeric.
create table table1 (
ID integer
Gender varchar(10),
Team numeric
);
insert into table1 (ID,Gender,Team) values
(1,'M',NULL),
(2,NULL,10),
(3,NULL,6),
(4,''F',NULL),
(5,NULL,3);
I will like to create a new column as Nxt that returns a row that is not null from any of the columns either a string or integer.
The column Nxt will look like: M,10,6,F,3
I tried this:
select coalesce(Gender,Team) as value from table1;
It returns this error:
COALESCE types character varying and numeric cannot be matched
Try to cast the column as text
select coalesce(Gender,Team::text) as value from table1;

Unable to change field type to not null with default value

I have a simple SQL statement, which looks like so:
alter table my_table alter column my_field set data type numeric(12,4) not null default 0;
But I get an error message, that points to not. What is wrong with that?
Use separate ALTER COLUMN clauses for the type, null behavior, and default value:
ALTER TABLE my_table
ALTER COLUMN my_field TYPE numeric(12,4),
ALTER COLUMN my_field SET DEFAULT 0,
ALTER COLUMN my_field SET NOT NULL;

postgresql use column name value when quoted with single quotes

I'm trying to update hstore key value with another table reference column. Syntax as simple as
SET misc = misc || ('domain' => temp.domain)
But I get error because everything in parenthesis should be quoted:
SET misc = misc || ('domain=>temp.domain')::hstore
But this actually inserts temp.domain as a string and not its value. How can I pass temp.domain value instead?
You can concatenate text with a subquery, and cast the result to type hstore.
create temp table temp (
temp_id integer primary key,
domain text
);
insert into temp values (1, 'wibble');
select ('domain => ' || (select domain from temp where temp_id = 1) )::hstore as key_value
from temp
key_value
hstore
--
"domain"=>"wibble"
Updates would work in a similar way.

Change column type and set not null

How do you change the column type and also set that column to not null together?
I am trying:
ALTER TABLE mytable ALTER COLUMN col TYPE character varying(15) SET NOT NULL
This returns an error.
What is the right syntax?
This should be correct:
ALTER TABLE mytable
ALTER COLUMN col TYPE character varying(15),
ALTER COLUMN col SET NOT NULL
Also, if you want to REMOVE NOT NULL constrain in postgresql:
ALTER TABLE mytable
ALTER COLUMN email DROP NOT NULL;