Wednesday, March 28, 2012

Restrict certain characters in varchar column

Hi - Is it possible to restrict the domain of a varchar column to
prevent certain chatracters being accepted. I want to prevent commas.
ThanksYou could use a check constraint:
alter table <table>
add constraint <constraint name>
check (patindex('%,%', <column> ) = 0)
go
ML
http://milambda.blogspot.com/|||hals_left (cc900630@.ntu.ac.uk) writes:
> Hi - Is it possible to restrict the domain of a varchar column to
> prevent certain chatracters being accepted. I want to prevent commas.
ALTER TABLE tbl ADD CONSTRAINT nocommas CHECK col NOT LIKE '%,%'
Then again, this sounds like a somewhat strange business rule. Semicolons,
periods, colons etc are OK, but not commas? What is the real story?
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||>What is the real story?
When I pass CSV strings to a stored proc, a comma inside a value causes
a problem.
I will re-write the split code to use some other character|||Dejan Sarka posted a very nice function that you might find use for:
http://solidqualitylearning.com/blo.../10/22/200.aspx
ML
http://milambda.blogspot.com/sql

No comments:

Post a Comment