Link to home
Start Free TrialLog in
Avatar of boris711
boris711

asked on

Firebird DB - need to update column values

Hello experts!

Due to a messy import of data from another database we need an SQL statement with Firebird syntax that will update the numeric values in entire columns of specific tables by rounding to two decimal places. I am using Database Workbench to access the database, but don't know the proper SQL command / script to run to adjust the values as stated above.  I also know that there are a probably a dozen post here that could help me, but I don't have time to keep looking for them.

I have table called JOBS, with columns called COST, AVGCOST, ESTCOST to name a few.  

TIA!
Avatar of Nick Upson
Nick Upson
Flag of United Kingdom of Great Britain and Northern Ireland image

update jobs set cost = cast(cost as decimal(15,2)), ....... where

please check before doing on the live data that you get the rounding you want
Sorry NickUpsons but your command will not round but just cut numbers...
For example:   1.566 will become 1.56
I think that boris711 need to round numbers so he shoud get 1.57
But I think the solution will need special UDF.

Regards
  Daniel

if so you can fix by using
update jobs set cost = cast((cost + 0.005) as decimal(15,2)), ....... where
Avatar of boris711
boris711

ASKER

Thank you for your response, I will test this command.
One more thing.
When "cost" will be negative you need to subtract those 0.0005
Like that:
   cast((cost - 0.005) as decimal(15,2))  .... where cost <0
It would appear to me that the above recommendations would change the datatype, maybe I'm wrong. It just seems like this should be easier.

Just to clarify, I am new to SQL and am having problems finding adequate (newbie) reference for the Firebird (we are using v1.5, not by our choice) flavor in particular. I own "The Firebird Book" but have found it most useful as a doorstop, probably due to my current level of knowledge.

I believe this SQL statement would do what I require in other SQL flavors:
Update ITEM Set avgcost = Round(avgcost, 4)

But in Firebird it would appear that there is no Round function. Can anyone tell me what the equivalent FB command would be?
so what exactly does this function do, round up, down, apply bankers rounding rules or .....
I believe this function will round to four decimal places all the values in column (avgcost) in table (ITEM). I admit, I could be wrong, but that's why I am asking the question in the first place.
you need to decide exactly what rounding you want, e.g. 3.12345 - do you want that as 3.1234 or 3.1235
the typical rounding...3.12345 to 3.1235
ASKER CERTIFIED SOLUTION
Avatar of Nick Upson
Nick Upson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thx, Nick, will give it a try on my test db.
For anyone else that might need it, this is the Firebird SQL command as run from within the Database Workbench SQL Editor. It worked exactly as required, performing standard rounding. If = or > 5 then round up, else round down.  

update <table_name> set <column_name> = cast (<column_name> as decimal(15,< #_dec_places>)) where <column_name> > 0.00;

It did not change the datatype of the column, as it appears it might. Also, if you remove "where <column_name> > 0.00" it works correctly on negative numbers.

Thanks for your time, Nick.