Sunday, February 19, 2012

Best way to restrict a varchar field not to contain spaces?

Hi,

is this the best way?

ALTER TABLE MyTable
ADD CONSTRAINT CK_MyTable_NoSpace
CHECK (charindex(' ', MyColumn)=0)

Is there any other way?

Thnx

Not sure but you can also try NOT LIKE '% %'

Try them both. What is the reason you need this?

|||

Thanks Simon

SimonSa wrote:

NOT LIKE '% %'

It would have to be trimmed first no?

or maybe:

NOT LIKE '% %' AND NOT LIKE '% ' AND NOT LIKE ' %'

SimonSa wrote:

What is the reason you need this?

File system entries. The OS allows them but I want to restrict them. It is also being done in the DAL.

|||

Carl M. wrote:

Thanks Simon

SimonSa wrote:

NOT LIKE '% %'

It would have to be trimmed first no?

or maybe:

NOT LIKE '% %' AND NOT LIKE '% ' AND NOT LIKE ' %'

No. % can stand for any number of characters, even none. NOT LIKE '% %' would be correct.

|||Thank you.

No comments:

Post a Comment