简体   繁体   中英

Migrate MS-Access tables with Regular Expression in Validation Rule to SQL-Server

I'm migrating a MS-Access.accdb-database to SQL Server 2017. In my MS Access database, there is a lot of table columns with validation rule like "NOT LIKE [.a-z0-9.] ". I cannot find any way to add a constraint in SQL Server that do the same thing. Is there a way to do this, or is it simply impossible to migrate MS Access to SQL-Server without losing functionality?

ALTER TABLE [Zones]
    ADD CONSTRAINT [CHK_Zon]
        CHECK ([Zon] NOT LIKE '*[!a-z0-9.]*')

I think you want:

NOT LIKE '%[^a-z0-9.]%'

This will mean that only the letters a to z , the numerals 0 to 9 and the character . will be allowed. Note that all or some upper case letters may be allowed, depending on your collation, and all lowercase letters, with az .

% are multi-character wildcards.

The ^ (carat) symbol means "Any single character not within the specified range" . So, for example, 'Test' NOT LIKE '%[^Az]%' would return TRUE, as it only contains the characters in the range Az (it doesn't contain any outside of it.

SQL Server does not support Regex (not natively anyway, it can by use of CLR functions), it only has simply pattern matching; which you can read about here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM