We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

help with an update statement and CAST()

Arana (G.P.)
Arana (G.P.) asked
on
Medium Priority
1,034 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)


Comment
Watch Question

Principal Operations Engineer
CERTIFIED EXPERT
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT

Commented:
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)"
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';
CERTIFIED EXPERT

Author

Commented:
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
CERTIFIED EXPERT
Commented:
>>>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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.