Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Limiting the Possible Values in a Text Column

Posted on 2004-08-30
6
Medium Priority
?
293 Views
Last Modified: 2011-10-03
I have a column in a 2000 SQL Server Database that is of the text data type.  I would like to restrict the possible values to either 'blue', 'red', or 'green'.  Any help is appreciated, and I will reward points quickly.
0
Comment
Question by:LucasWhite84
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:LucasWhite84
ID: 11932639
In other words, I want the database to have a constraint where it won't allow any more values other than 'blue', 'red', and 'green' to be placed in this column.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11932727
You can use hte following script - modifying fro your table / column names of course :

alter table mytable add constraint [CK_mytable]  CHECK ([text_field] = 'red' or [text_field] = 'green' or [text_field] = 'blue')
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 1600 total points
ID: 11932746
Or you can do it using Enterprise Manager - right click on table -> design table
in the table designer, right-click and select Check Constraints
there you acn enter the constraint name e.g. CK_mytable, and the text e.g. [text_field] = 'red' or [text_field] = 'green' or [text_field] = 'blue'
0
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:LucasWhite84
ID: 11932878
I tried to run the script and it said...

Server: Msg 1760, Level 16, State 1, Line 1
Constraints of type CHECK cannot be created on columns of type TEXT.

Then, I tried to type in [Color] = 'red' or [Color] = 'green' or [Color] = 'blue' in the Enterprise Manager constraint entry and it said that there was an error with the constraint.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 400 total points
ID: 11932983
>>Constraints of type CHECK cannot be created on columns of type TEXT.<<
If all you need to save is red green or blue.  Than you should not be using TEXT data type in the first place.  Use varchar(5) instead.
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11933007
Yes, as the error says, Constraints of type CHECK cannot be created on columns of type TEXT.
Sorry, I didn't notice that you had text data types.
TEXT data types are really only for large text strings (normally > 8000 characters) they have a lot of limitations and are really desinged only for extra long strings.
what you want is data type char or varchar. You should change your column type to something like varchar(5) if the only valid values are 'red'. 'blue' or 'green'
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

705 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