Home » RDBMS Server » Server Administration » Updating records in a Table
Updating records in a Table [message #372732] Tue, 06 March 2001 10:03 Go to next message
Arpit
Messages: 99
Registered: March 2001
Member
Hi,

I need some help in the SQL script. I'm tyring to update my "Contact" table which has
"POSTAL_ZIP_CODE column with postal code records having character 'O' instead of number '0' in it.

Example: MOR ON1 --->M0R 0N1

Can you please help me with the related SQL script to update the table. Any help on this asap would highly
be appreciated.

Thanks!
Alpesh
Re: Updating records in a Table [message #372737 is a reply to message #372732] Tue, 06 March 2001 10:24 Go to previous messageGo to next message
kavithask
Messages: 34
Registered: March 2001
Location: London
Member
Hi,

Use the translate command and replace all occurrences of "O" or "o" to Zero.

update tablename
set fieldname = translate(fieldname, 'Oo', '0')
/

HTH
Kavitha
Kavitha
Re: Updating records in a Table [message #372738 is a reply to message #372732] Tue, 06 March 2001 10:25 Go to previous messageGo to next message
Andrew again...
Messages: 270
Registered: July 2000
Senior Member
select translate('MOR ON1', 'O' ,'0') from dual;

update CONTACT
set POSTAL_ZIP_CODE = translate('MOR POSTAL_ZIP_CODE', 'O' ,'0')
where POSTAL_ZIP_CODE like '%O%';
Re: Updating records in a Table [message #372739 is a reply to message #372732] Tue, 06 March 2001 10:35 Go to previous messageGo to next message
Arpit
Messages: 99
Registered: March 2001
Member
Hi Andrew,

Thanks for your reply back.

Will the above constraint prevent the users from entering in char 'O' instead of number'0' in postal code field. Please confirm.

Thanks!
Alpesh
Re: Updating records in a Table [message #372740 is a reply to message #372732] Tue, 06 March 2001 10:56 Go to previous messageGo to next message
Andrew again...
Messages: 270
Registered: July 2000
Senior Member
Yes. Try to make sure that your App gives the user a meaningful error message. If you cant change the app, then naming the costraint like CANT_HAVE_O_IN_CODE could help if only the violated constraint will be displayed to the user.

You can expand the constraint like
(length(POSTAL_ZIP_CODES) = length(rtrim(ltrim(PSOTAL_ZIP_CODE)))
and POSTAL_ZIP_CODES = upper(POSTAL_ZIP_CODES)
and length(POSTAL_ZIP_CODES) >= 5) etc.
Re: Updating records in a Table [message #372741 is a reply to message #372732] Tue, 06 March 2001 11:28 Go to previous messageGo to next message
Arpit
Messages: 99
Registered: March 2001
Member
Hi Andrew,
Thanks ! once again.

Last question--How can we find what all constraints are there on a table and is there any way we can define a message for the constraint ?

Thanks!
Alpesh
Re: Updating records in a Table [message #372745 is a reply to message #372741] Tue, 06 March 2001 17:04 Go to previous message
Andrew again...
Messages: 270
Registered: July 2000
Senior Member
SELECT owner, constraint_name, table_name, search_condition, r_owner, r_constraint_name,
delete_rule, status,
DECODE (constraint_type,
'U', 'Unique Key',
'C', 'Check',
'P', 'Primary Key',
'R', 'Referential Integrity',
'V', 'Check Option on a view'
) constraint_type
FROM user_constraints
WHERE generated = 'USER NAME';

Note, this won't show constraints which have system generated names like SYS_nnnnn, there will be lots of them, especially not null constraints which you may not even be aware are being created.

As far as the message is concerned, no I dont think the constraint can give you a meaningful message othe than the name of the constraint.

You could try something like this in an combined Insert and Update trigger rather than a check constraint too...

if upper(new:POSTAL_ZIP_CODE) like '%O%' then
RAISE_APPLICATION_ERROR(-20501, 'ERROR: Postal Zip Code cant contain the letter O, check Postal Zip Code.', true);
end if;

This will give you the message you are after if errors are redirected to the user (they often are).
Previous Topic: Re: who to search patterns ([0-9] or [a-z]) in sql
Next Topic: SQL Plus
Goto Forum:
  


Current Time: Sat Jun 29 01:28:50 CDT 2024