IT Community - Software Programming, Web Development and Technical Support

Constraint to allow all numers and alpha

This is a discussion on Constraint to allow all numers and alpha within the Database Support forums, part of the Web Development category; Does anyone know how to develop a check constraint in oracle that will only allow digits 0-9 and a ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 08-06-2007, 12:40 PM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Default Constraint to allow all numers and alpha

Does anyone know how to develop a check constraint in oracle that will only allow digits 0-9 and a dash? Thank you ahead of time.
__________________
H2O

Without us, no one can survive..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 11:21 PM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: Constraint to allow all numers and alpha

Hi,

Refer the details given below..

Quote:
CREATE TABLE check_test
(
id INTEGER,
value VARCHAR2(3),
CONSTRAINT chk_value CHECK(value IN ('0', '1','2','3','4','5','6','7','8','9','-'))
);
Table created


INSERT INTO check_test(id, value) VALUES (1, '2')
1 row inserted
INSERT INTO check_test(id, value) VALUES (1, '9')
1 row inserted
INSERT INTO check_test(id, value) VALUES (1, '-')
1 row inserted


INSERT INTO check_test(id, value) VALUES (1, '10')
ORA-02290: check constraint (PMXUSERS.CHK_VALUE) violated;

INSERT INTO check_test(id, value) VALUES (1, '--')
ORA-02290: check constraint (PMXUSERS.CHK_VALUE) violated
hope this will do... !!
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-07-2007, 01:27 AM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Constraint to allow all numers and alpha

Something like...

CHECK (LTRIM (column_name, '0123456789-') IS NULL)

...of course depending on version you may be able to use a regular expression here.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-07-2007, 01:29 AM
H2o H2o is offline
D-Web Analyst
 
Join Date: Jul 2007
Posts: 246
H2o is on a distinguished road
Default Re: Constraint to allow all numers and alpha

Is there a way to format it like a zip code, for instance the field is zip code, I want to allow either a format like this 99999 or 99999-9999. Is there a constraint that will validate against either format and check for the "-" as well?

Please let me know and thank you for your answer!!!
__________________
H2O

Without us, no one can survive..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-20-2007, 07:12 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,321
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Default Re: Constraint to allow all numers and alpha

SQL>CREATE TABLE TEST_TABLE
2 (
3 number_and_ VARCHAR2(30)
4 CONSTRAINT p_check
5 CHECK ( REGEXP_LIKE ( number_and_, '^[0-9\-]*$' ) )
6 )
7 /

Table created.

SQL>insert into TEST_TABLE values('-');

1 row created.

SQL>insert into TEST_TABLE values('0-9');

1 row created.

SQL>insert into TEST_TABLE values('0-9222-');

1 row created.

SQL>insert into TEST_TABLE values('0-9222-*');
insert into TEST_TABLE values('0-9222-*')
*
ERROR at line 1:
ORA-02290: check constraint (P_CHECK) violated
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-20-2007, 07:22 AM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,321
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Default Re: Constraint to allow all numers and alpha

Pls visit

4 Using Regular Expressions in Oracle Database
__________________
Prasanna Vignesh
MCPD | Web Developer
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-20-2007, 07:28 AM
suresh suresh is offline
D-Web Trainee
 
Join Date: Apr 2007
Posts: 22
suresh is on a distinguished road
Default Re: Constraint to allow all numers and alpha

> Is there a way to format it like a zip code, for
> instance the field is zip code, I want to allow
> either a format like this 99999 or 99999-9999. Is
> there a constraint that will validate against either
> format and check for the "-" as well?
>
> Please let me know and thank you for your answer!!!


create table test (str varchar2(100) constraint t_check check(translate(str,'012345678','999999999') in ('99999','99999-9999')))
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/database-support/3168-constraint-allow-all-numers-alpha.html
Posted By For Type Date
DiscussWeb IT Community - Technical Support and Technology Discussions This thread Refback 08-20-2007 08:12 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is alpha an beta testing? devarajan.v Software Testing 1 07-16-2007 10:52 PM


All times are GMT -7. The time now is 06:41 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0