# How can I change an already populated column from varchar to decimal in Base? [closed]

I mistakenly set up a table with a column as varchar although it would only contain decimal numbers. I thought I could get away with not changing the datatype, it's a split database and at the time I thought I couldn't be bothered with any sql. But now I need to multiply that column's values by a fixed number which is not possible probably because of the data type. When I try to change the data type with this: ALTER TABLE "Table1" ALTER COLUMN "Column2" DECIMAL (3,2) I get the following error message: data exception: invalid character value for cast in statement [alter table "Table1" alter column "Column2" decimal (3,2)] Is there something wrong with my syntax or is the error due to the fact that I am trying to convert an already populated column? Is there an easy way of converting the column without losing any data?

edit retag reopen merge delete

### Closed for the following reason the question is answered, right answer was accepted by Alex Kemp close date 2020-09-16 14:26:58.788978

Sort by » oldest newest most voted

There are some problems with a split DB I'll try to explain.

First, running a test with your setup only gave me a problem if using such a small length for the DECIMAL field. Increasing it to 10,2 worked without problem.

After the ALTER statement, if you edit the table, it appears that the field hasn't changed. It actually has and if you run an SQL statement with a calculation on the field, you will see it works. To see the actual modified version of the table, run the following SQL command (of course with your table name):

SELECT * FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = 'Your table name here' ORDER BY "ORDINAL_POSITION"


You will then see the field has actually changed.

more

Changing field length did not seem to have any effect. I tried a calculation to check like you suggested but got the same error message. Where do I run that sql command you quoted? Running it in the main BASE window's Tools>SQL... gave me a result of "]".

( 2016-12-10 08:31:44 +0100 )edit

I think you might have entered alter table "Table1" alter column "Column2" decimal (3,2)] instead of alter table "Table1" alter column "Column2" decimal (3,2)

( 2016-12-10 08:37:16 +0100 )edit

@a_lega your comment is unclear. Did the ALTER statement with a larger field work (command completed successfully) or did it fail? Based on the comment, you tried a calculation after the change, so it seems you are saying it worked. If it did fail, you probably have non-numeric characters somewhere in that column which must be removed in order to run the ALTER command successfully. The SELECT statement is easiest to see results in QUERY.

( 2016-12-10 16:47:20 +0100 )edit

I am sorry for the unclear comment. I tried the following SQL command: ALTER TABLE "Table1" ALTER COLUMN "Column2" (10,2) The command failed. I tried the calculation in case the alteration had registered anyway, despite the error message (I know that's not what you meant), which ofcourse also failed. I suppose what I have to do is run a query with a criterion that would show me records with hidden gaps or non num chars?

( 2016-12-11 17:16:33 +0100 )edit

it took a bit and I'm certainly no expert with SQL but this worked for me to find records that were other than numeric:

SELECT ID, T1."Your-Field-Name" FROM "your-Table-Name" T1
LEFT JOIN (select "Your-Field-Name" from "Your-Table-Name" where (CASE WHEN         REGEXP_MATCHES(TRIM("Your-Field-Name"), '[0-9]+') THEN CAST (TRIM("Your-Field-Name") AS     INTEGER) ELSE NULL END) > '0') T2 ON T2."Your-Field-Name" = T1."Your-Field-Name" WHERE     T2."Your-Field-Name" IS NULL

( 2016-12-11 19:53:14 +0100 )edit

Run as Query - 'Run SQL command directly' turned on. Result is records with spaces or nun-numeric characters which must be corrected.

( 2016-12-11 19:56:09 +0100 )edit

Unfortunately, if your database is embedded, the SELECT statement will NOT work. You must be using a newer HSQL. If this is the case I have no further suggestions at this time.

( 2016-12-11 20:42:58 +0100 )edit

I ran that sql and got this error message: Κατάσταση SQL: 42501 Κωδικός σφάλματος: -5501

( 2016-12-13 08:56:03 +0100 )edit

My database is split and I am using Libreoffice 5.2.1.2 (x64), by the way.

( 2016-12-13 08:59:35 +0100 )edit
1

Change ID to a field name in your table where you can uniquely identify records.

( 2016-12-13 18:44:33 +0100 )edit

Guessing you have some text which can't be automatically converted. Hard to say what it is. Could be a leading space or something.

First, suggest you sort out if your alter command works at all. So create a simple test database and try your alter command on it as follows:

1) while it's empty,
2) with just one record having an integer,
3) then with a decimal like 4.23,
4) then with the string " ", etc.

Perhaps it will come to you what's going on.

Failing that, usually when I get stuck like you are, I just create a new field, and figure out how to copy my values to it, stumbling over what I must to move the data, either manually, via a SQL update query or sometimes using a Basic program to manually convert and copy the data from old field to new field, then abandon and delete the original field. When I'm done deleting the old field I rename the new field to the old field's name. Yes, I know this is a bit of trouble, but it is what has worked for me on simple and harder fixes.

I am still learning how to do some of this myself on Base, but these are the methods I have used for many years on Access and many other data bases before that.

more