?
Solved

help with an update statement and CAST()

Posted on 2006-04-03
7
Medium Priority
?
1,015 Views
Last Modified: 2013-12-09
i have a table wich i need to update
my intention is to update the field increasing 100 to its current value, unfortunately that value is stored as string

for example '0165' would like to turn it into "0265'
so my approach was to use cast to turn that into a value increase it by 100 then turn it into a character and padding it with a 0, but the next statement is not working, im very new to firebird and i was looking for a TONUMBER or VAL function, im most used to oracle and/or mssqlserver

update CHECADAS
set
 CHECADAS.CH_H_REAL='0'||cast(CAST(CH_H_REAL AS  NUMERIC)+100 as character(800))
where
  CH_H_AJUS = '0630';



please help this is urgent, that system didnt consider day time savings and i need to update the records (check in time values) for all those who checked-in  with a wrong time (one hour ahead of time)


0
Comment
Question by:arana
5 Comments
 
LVL 19

Accepted Solution

by:
NickUpson earned 336 total points
ID: 16365889
update CHECADAS
set
 CH_H_REAL='0'||cast(CAST(CH_H_REAL AS  INTEGER)+100 as char(800))
where
  CH_H_AJUS = '0630';

if this doesn't work, please explain exactly what does happen, error message or ?

I've changed numeric to integer as cast to numeric will give 630.00
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16369559
What type is the field CH_H_REAL?

Assuming it is of CHAR(4), you should rather casting to "char(3)" instead of casting to "char(800)"
0
 
LVL 19

Assisted Solution

by:Gary Benade
Gary Benade earned 332 total points
ID: 16424879
I assume you want to change all the 0630 (a time?) to 0730, so why dont you just do this?

update CHECADAS
set
 CH_H_REAL = '0730'
where
  CH_H_AJUS = '0630';
0
 
LVL 11

Author Comment

by:arana
ID: 16440475
this was so urgent it was solved by editing one by one from the client application (about 300 records only affortunately) so even if any of the suggestion works its of of no use right now, but i  still want the solution just to know why this damn thing isnt working and maybe learn something along the way.

tried the INTEGER approach

"I've changed numeric to integer as cast to numeric will give 630.00"
i tried this, this is the error:
with numeric:
*****************************************************
ISC ERROR CODE:335544321

ISC ERROR MESSAGE:
arithmetic exception, numeric overflow, or string truncation

Statement: update CHECADAS
set
 CH_H_REAL='0'||cast(CAST(CH_H_REAL AS numeric)+100 as char(800))
where
 CH_H_AJUS = '0630'
******************************************************

with integer:
**************************************************
ISC ERROR CODE:335544321

ISC ERROR MESSAGE:
arithmetic exception, numeric overflow, or string truncation

Statement: update CHECADAS
set
 CH_H_REAL='0'||cast(CAST(CH_H_REAL AS INTEGER)+100 as char(800))
where
 CH_H_AJUS = '0630'
***************************************************

tried both with CHAR(3) and same thing happens




"What type is the field CH_H_REAL?"
CHAR(4)


"I assume you want to change all the 0630 (a time?) to 0730, "
your assumption is not right, 0630 is another column, we dont know the value of ch_h_real.
i need to add 100 to ch_h_real value (no matter what it is) whenever the ch_h_ajus is 0630
0
 
LVL 14

Assisted Solution

by:Pierre Cornelius
Pierre Cornelius earned 332 total points
ID: 16526291
>>>but i  still want the solution just to know why this damn thing isnt working and maybe learn something along the way.

You will get errors when casting to char(800) as your field can only hold 4 chars.

You say you got the error even when casting to Char(3)? My best guess is that the problem lies with the other part of your statement then i.e.  the part "CAST(CH_H_REAL AS INTEGER)". Most likely you have certain rows with null values so the error results because it cannot cast a null value to an integer.

The following probably would have sorted out your problem:

set
 CH_H_REAL='0'||cast(CAST(CH_H_REAL AS INTEGER)+100 as char(800))
where
 CH_H_AJUS = '0630' AND CH_H_REAL is not null

Regards
Pierre
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question