Postgres: Rewrite boolean datatype to numeric - sql

I have a database with a lot of tables which contain many columns of type boolean. But my software doesn't support boolean as a datatype. Their support team told me that I should use numeric(1,0) instead of boolean, so I tried:
ALTER TABLE foo ALTER COLUMN bar TYPE numeric(1,0) USING bar::numeric(1,0)
I get an error: can't convert boolean into type numeric.
What can I do to replace the boolean columns with a numeric alternative and preserve the boolean behaviour? Can this numeric(1,0) thing even work?

you need an extra step - cast to int first:
t=# select true::int::numeric(1,0);
numeric
---------
1
(1 row)
so in your case it would be:
ALTER TABLE foo ALTER COLUMN bar TYPE numeric(1,0) USING bar::int::numeric(1,0)
but frankly saying I believe just converting to int is already enough.

Related

Convert a type to another in Postgres

I have a table of cars in my Postgres database, but a created the price column with varchar.
I want to convert this type to numeric or integer.
car database
You can change the table definition with ALTER TYPE, but you'll need a USING clause with your particular conversion. Related:
Rails Migrations: tried to change the type of column from string to integer
If your data sample tells the full story, this should work:
ALTER TABLE tbl
ALTER COLUMN price TYPE numeric USING right(price, -1)::numeric
right(price, -1) trims the first character, which is always $ in your sample. Remaining leading and trailing white space is no problem. Related:
Postgres data type cast
For anything else, adapt the expression.
The operation triggers a table rewrite, taking an exclusive lock on the table for the duration.
I chose numeric to cover fractional digits. See:
Which datatype should be used for currency?

How to alter datatype of a column in BigQuery

I'm trying to change the datatype for a column in my bigquery table from INT64 to STRING with the condition it's not NULL.
When I type:
ALTER TABLE table_name ALTER COLUMN id STRING NOT NULL
I get an error
Syntax error: Expected keyword DROP or keyword SET but got identifier "STRING"
How should I resolve this?
It is unsupported to change a column's data type at the moment.
Take a look at the official documentation. It explains 2 ways to manually change a column's data type. For the record:
Using a SQL query: choose this option if you are more concerned about simplicity and ease of use, and you are less concerned about costs.
Recreating the table: choose this option if you are more concerned about costs, and you are less concerned about simplicity and ease of use.
Despite the fact that you have got the error due to not using SET:
ALTER TABLE table_name
ALTER COLUMN id SET DATA TYPE STRING
but anyway, unfortunately, it's not possible to alter from INT64 to STRING directly.
What you can do is create a new table using
CAST(id AS STRING) id

PostgreSQL - Cast generated column type

I tried to create generated column from other JSON type column with
ALTER TABLE "Invoice" ADD COLUMN created Integer GENERATED ALWAYS AS (data ->> 'created') STORED;
When I execute this I get error
ERROR: column "created" is of type integer but default expression is of type text HINT: You will need to rewrite or cast the expression. SQL state: 42804
I tried to cast it with CAST function and :: operator but with no lack. Is there any way to do it? Or maybe I should generate this column differently?
Thanks
How about converting the value to an int?
ALTER TABLE "Invoice" ADD COLUMN created Integer
GENERATED ALWAYS AS ( (data ->> 'created')::int ) STORED;

add bool column within existing table

I am trying to dd a column, to a database with the program Microsoft SQL Server Management Studio.
I already have a database, with a table, in that table i need to add another column.. but it keeps saying it cannot find type bool or boolean.
my code :
ALTER TABLE table_name ADD IsOpen boolean GO
Any ideas ?
side question, any idea how to alter an existing column ? i have a column called "budget" but it needs to be "Budget".
You need to use bit instead of bool datatype
ALTER TABLE table_name ADD IsOpen bit
GO
Here some info about datatypes
Data type Access SQLServer Oracle MySQL PostgreSQL
boolean Yes/No Bit Byte N/A Boolean
Answer for Qustion 2
The syntax to rename a column in an existing table in SQL Server (Transact-SQL) is:
Syntax:
sp_rename 'table_name.old_column_name', 'new_column_name', 'COLUMN';
For your column :
sp_rename 'table_name.budget', 'Budget', 'COLUMN';
bit (0/1) is used as boolean in SQL
ALTER TABLE table_name ADD IsOpen bit GO
ALTER TABLE [dbo].[TableX] ADD IsOpen bit
In mssql there is no type called boolean or bool. You have to use bit, which is 0 or 1.
The answers here are good but there's a best practice that I'm surprised hasn't been mentioned. I would recommend making all new boolean/bit columns NOT NULL.
By allowing null values, the value you're storing can be true, false, or null. In many boolean cases, having a third value (null) doesn't make sense and has the potential to cause unforeseen bugs.
This is the SQL script I would recommend for creating bit/boolean columns:
ALTER TABLE table_name ADD IsOpen bit NOT NULL DEFAULT 1
GO
This will not allow null values and will default records to have a 1/true value. If you expect your data to be false/0 by default, then just change the 1 here to a 0.

Casting a string as decimal in PSQL

I'm trying to convert a string to a decimal using this command:
SELECT cast(minimum_ticket_price AS DECIMAL(6,2)
FROM all_event_details
WHERE minimum_ticket_price ~ E'^\\d+$';
But this doesn't actually update anything in my database. It just displays the selected column in my terminal. Do I need to combine the select with an update? I've tried that but I must have the syntax wrong as I'm not able to get the conversion saved in the database.
Here's what I tried:
UPDATE all_event_details
SET minimum_ticket_price = cast(minimum_ticket_price AS DECIMAL(6,2))
WHERE ( minimum_ticket_price <> '') IS TRUE;;
Updating to a data type which minimum_ticket_price column's data can support is possible otherwise it will give an error.
for example if minimum_ticket_price column data type is varchar then your code must work.
What are you doing?
First add new column, decimal(but I'm goint to suggest to use the basics data type such real or double precision, are most efficient)
ALTER TABLE my_table ADD COLUMN minimum_ticket_priceR real ;
than
UPDATE all_event_details
SET minimum_ticket_priceR = to_number(coalesce(minimum_ticket_price,"0"),"999999D99") --for each row
than I'm going to suggest to drop the colum minimum_ticket_price and rename the other column (ever with ALTER TABLE):
What you did is not to understand, if minimum_ticket_price is string, you cannot set a number... and if is a number has no meaning to set it as string