I need to update a version column in a table from VARCHAR to int.
The version column is currently saving versions as a combination of a "v" and a number (e.g. v1, v2, v3), and I would like to have it just be the number.
I tried to use this sql query to update the table column:
alter table vanities alter column version TYPE INTEGER USING(version::integer);
However, I got the following error:
[22P02] ERROR: invalid input syntax for integer: "v1"
Is there a SQL query I could use that would strip the column of all of the v characters and then convert it into an integer?
Try getting rid of the first character:
alter table vanities alter column version TYPE INTEGER USING (substr(version, 2)::integer);
Here is a db<>fiddle.
Related
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;
I have a table with a string column. I convert this column to a number using the function TO_INTEGER(). Ist work fine. But If I Aggregate the converted column with the function SUM I got this error:
SAP DBTech JDBC: [339]: invalid number: not a valid number string ' ' at function to_int()
This is my sample SQL query:
select SUM(PARTICIPANT)
from (
select TO_INTEGER(STUDENT) as PARTICIPANT
from MyTable)
Column STUDENT is a varchar(50) in MyTable
What did I do wrong?
Thanks in advance
Without seeing your Column values, it looks like you're trying to convert the numeric sequence at the end of your values list to a number, and the spaces that delimit it are throwing this error. But based on the information you've given us, it could be happening on any field.
Eg:
Create table Table1 (tel_number number);
Insert into Table1 Values ('0419 853 694');
The above gives you a
invalid number
Kindly Check Filter/ where clause if you try to give string column value as integer that time you got this error message. I wrote in HANA - Calculation view - SQL Script, In where clause Bukrs (Company Code) = 1000 after that i changed Bukrs = '1000'. then this issue is resolved.
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
Here, I am only going to get CheckListNo (integer) from select statement "Check_No" ( character varying). when am execute this, error showing like this 'operator does not exist: character varying = integer'. So, I want to check without changing datatypes.
For an example, SELECT "Check_No"
FROM "Project_CheckList_Options" let we see I defined "check_No" column as a integer if I want to get records in character varying. I can use just "check_No":: character varying then I can see column records in character varying type without affecting integer datatype. So like that Is it possible to convert datatype when alter the table with check constraint in postgresql?
alter table "Project_Configuration"
add check("CheckListNo" in (SELECT "Check_No" FROM "Project_CheckList_Options"))
I am very new to SQL/Hive and trying to set a maximum length for strings in a column when creating my table as below,
hive> CREATE TABLE Persons
(
PersonID int,
Suffix string(5),
LastName string,
FirstName string
);
FAILED: ParseException line 3:15 mismatched input '(' expecting ) near 'string' in create table statement
Any ideas what I am doing wrong?
Till Hive version 0.11 you cannot restrict the length of string column. You have to use STRING data type for string column.
But from Hive version 0.12 we have varchar data type just like other RDBMS where you can specify and restrict the length of string columns. You check the data types in hive here: data types in hive
Also for create table syntax in Hive you refer to this link : Create table syntax in Hive
Hope this helps..!!!