Ask Your Question

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

asked 2016-12-09 18:52:18 +0100

a_lega gravatar image

updated 2016-12-09 19:32:18 +0100

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 flag offensive 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

2 Answers

Sort by » oldest newest most voted

answered 2016-12-09 20:20:35 +0100

Ratslinger gravatar image

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):


You will then see the field has actually changed.

edit flag offensive delete link 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 "]".

a_lega gravatar imagea_lega ( 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)

EasyTrieve gravatar imageEasyTrieve ( 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.

Ratslinger gravatar imageRatslinger ( 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?

a_lega gravatar imagea_lega ( 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
Ratslinger gravatar imageRatslinger ( 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.

Ratslinger gravatar imageRatslinger ( 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.

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

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

user lacks privilege or object not found: ID

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

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

a_lega gravatar imagea_lega ( 2016-12-13 08:59:35 +0100 )edit

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

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

answered 2016-12-09 19:43:08 +0100

EasyTrieve gravatar image

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.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-12-09 18:52:18 +0100

Seen: 2,337 times

Last updated: Dec 09 '16