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



Answer this question

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

  • kjmorford

    Thank you.
  • Eugenia

    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.


  • ScubaRon

    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.


  • Adam Plocher

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

    Try them both. What is the reason you need this



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