1

I am trying to implement some constraints on tables so that there is no tampering of the data from what is expected.

One of the constraints is on a varchar column. It should at least contain one non-numeric character.

How can I implement this constraint in SQL Server, and if I implement it, What will be the effect on the performance if any?

1 Answer 1

2

A check constraint with a LIKE expression could be used to ensure at least one non-numeric character exists:

CHECK (YourColumn LIKE '%[^0-9]%')

The 0-9 range there might not be exactly right for you, depending on the collation of the varchar column. For example, using the common Latin1_General_CI_AS collation, the following additional characters would be considered numeric by the 0-9 test:

²
³
¹
¼
½
¾

If that is a concern, expand the range explicitly:

CHECK (YourColumn LIKE '%[^0123456789]%')

I would expect negligible performance impact in either case.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.