Constraint with regexp - sql

I am currently trying to create a regular expresion to use in my Oracle database constraints' that is able to check if all the words of a string starts with an uppercase. I already tried to use functions like initcap, but it really doesn't work as expected, because it gives an error when I try to insert values that contains a letter like "ñ" or an acent. Could you please help me?

I think the check constraint that you want is more like this:
check (not regexp_like(mycol, '(^|\W)[[:lower:]]'))

You could do this with a check constraint and a regex, like so:
alter table mytable
add constraint myconstraint
check (not regexp_like(mycol, '(^|\W)[a-z]'))
Regexp (^|\W)[A-Z] means: the beginning of the string or a non-word character followed by an lower case value. not regexp_like(...) forbids such pattern.
Demo on DB Fiddle
As commented by #kfinity, you can also use regex (^|\W)[[:lower:]], that should properly trap special upper characters.

Related

INITCAP in MariaDB

I am currently trying to check that every values that are inserted into my MariaDB table the 1st letter of every word in uppercase, so I tried to use CONSTRAINT nombreok CHECK (INITCAP(nombre_director) = nombre_director)); but it doesn't works because it gives me an error saying that function INITCAP() cannot be used in a CHECK... what can I do to solve it?
I think initcap() is not deterministic because the results may depend on the default collation of the database (under some circumstances).
You can ensure that the first characters are capitalized using:
CONSTRAINT nombreok CHECK (not (nombre_director collate latin1_general_cs) REGEXP '(^|[[:space:]])[[:lower:]]')
Here is a db<>fiddle.
If you want to check the lower case as well:
CONSTRAINT nombreok CHECK ((nombre_director collate latin1_general_cs) REGEXP '^([[:upper:]][[:lower:]]+([[:space:]]+|$))+$')
However, you might want to extend this beyond just alphabetical characters.
Here is a db<>fiddle for this.

how to restrict special character except space in a table in oracle

i have a requirement to insert data into a varchar2 column.can anyone please help me how to restrict special characters except space in a table field in oracle and also the field should not accept two consecutive space
You are looking for a check constraint, which seems to need regular expressions. Something like this:
alter table t add constraint chk_t_col
check (regexp_like(col, '^[a-zA-Z0-9 ]+$') and col not like '% %');
I'm not sure what the set of characters is, but the above is for alphanumeric and space.
Note: You can create the regular expression to preclude the double spaces. I find the above easier to follow.

HSQL Query constraint for alphabets and numbers

How I have a column Firstname which should be only Alphabets
and phone number which should be only numbers in HSQL
i wrote something like
ALTER TABLE USER
ADD CONSTRAINT CHECK_USER_FIRST_NAME CHECK (FIRST_NAME NOT LIKE '%[^A-Z ]%' )
But it allows Tp1 which i don't want.
Can someone help me with constraint in HSQL
LIKE doesn't support regular expressions. The only wildcards it supports are % for multiple characters and _ for a single character. To match against a regular expression you need regexp_matches()
ALTER TABLE user
ADD CONSTRAINT check_user_first_name
CHECK (regexp_matches(first_name, '[A-Z]+'))
This will allow only uppercase letters, so you probably want to use '[A-Za-z]+' instead. It also requires at least one character in the name. If you want to allow empty strings, change the + to *. This will still allow null values though.
Note that USER is a reserved keyword in SQL. You shouldn't create a table with that name. If you try this e.g. on Oracle or PostgreSQL it will fail. The name requires to be quoted - which is not a good idea. You should find a different name.

How to prepend(?) number in text field with '0'?

I have a text field in Microsoft Access which consists of only digits. I don't know if these digits are preceded by blank characters or not.
What I want to do is if the field has '1' I want to convert it to '0001', if it has '89' then I want to convert it to '0089', etc. Meaning I just want to make the field consistent in length of 4 characters and pad the number with the appropriate number of '0's.
How do I do this? Can I use the calculated field approach?
I can convert the database to SQL if SQL has an easy way to do this.
Thanks.
You can use the Format() function to transform the string of digits. Format() doesn't care whether or not the digits are preceded by spaces.
? Format("89","0000")
0089
? Format(" 89","0000")
0089
If you want to display those field values with that format in a query:
SELECT Format([YourTextField],"0000")
FROM YourTable;
If you want to change how they're stored:
UPDATE YourTable
SET [YourTextField] = Format([YourTextField],"0000");
Edit: #onedaywhen suggested a using CHECK CONSTAINT or Validation Rule to ensure your table only accepts valid data in the future. Here is a CHECK CONSTRAINT example:
ALTER TABLE YourTable
ADD CONSTRAINT four_digits_required
CHECK (
YourTextField LIKE '[0-9][0-9][0-9][0-9]'
);
You can run that statement from CurrentProject.Connection.Execute, which is an ADO object method. DDL statements with CHECK constraints can only be executed from ADO. So you could not execute that statement from CurrentDb.Execute, which is a DAO object method.
Alternatively this should work for YourTextField's Validation Rule property:
LIKE "[0-9][0-9][0-9][0-9]"
The Validation Rule approach would also allow you to use the Validation Text property to display a message to the user when submitted values are unacceptable:
"4 digits required."
In this situation the Validation Text approach is about the same as displaying the CHECK constraint name when that constraint is violated. However, if you wanted a more detailed message, the Validation Text could be a better choice.
You can try something like this:
RIGHT('0000' + TRIM(column), 4)
There are a few variations, here is another:
Left("0000", 4 - Len(Cstr(Trim(column)))) & Cstr(Trim(column))
Sometimes the Len command returns the length minus 1 with numeric values so the Cstr is there to avoid this.

How to use CHECK constraint to restrict non-numeric varchar2 (only alphabets)?

I have a column NAME
It must contain only characters and not numbers
How do I use CHECK condition:
CHECK(NAME NOT LIKE '%[0-9]%')
or any other method...
edit: Oracle database is used.
You didn't state your DBMS so I'm assuming PostgreSQL
CHECK(name ~ '^[^0-9]*$')
Double negative Should be standard (not MySQL though) because it uses LIKE:
CHECK(NAME NOT LIKE '%[^a-zA-Z]%')