How can i write check constraint [closed] - mysql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How can I write check constraint for one column say class_id where check condition should be: string started with CLS and followed by any 5 digit number.
for e.g: CL100987, CLS45678

try using regex
something like that
WHERE REGEXP_LIKE (COLUMN_CONDITION,'^CLS.*([0-9]{5})');
| REGEXP_LIKE -> like specific to regex | column_condition ->
explicit | '^**." -> start with and followed by any char |
([0-9]) - > only numerics | {5} -> the number of numerics in the
expression
pay attention to your dbms, some differences
hope this could help

Related

How can I add brackets to each word of a string which contains multiple words in SQL? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
How can I add brackets to each word of a string which contains multiple words in SQL? For example, I have:
string example = 'word1,word2,word3,word4,word5'
How can I convert it to something like this:
string example = '[word1],[word2],[word3],[word4],[word5]'
We can use a combination of REPLACE() and string concatenation:
SELECT val, CONCAT('[', REPLACE(val, ',', '],[') , ']') AS output
FROM yourTable;
On MySQL 8+, we can use a regex replacement:
SELECT val, REGEXP_REPLACE(val, '([^,]+)', '[$1]') AS output
FROM yourTable;

Mysql query to return regex match between two columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have two columns in a database I want to compare. One is email addresses and the other is username.
I'm looking to return results if the first part of username matches the first component of the email address (stuff before the # sign).
Sample data:
username emailaddress
badzzycshulzey9802 badzzycshulzey#gmail.com
trogleddg1919 trogleddg#gmail.com
Tried a variety of queries but I can't seem to get this one.... thanks!
You don't really need a regexp for this, strings functions can do it:
select *
from mytable
where username like concat(substr(emailaddress, 1, locate('#', emailaddress) - 1), '%')
locate('#', emailaddress) gives you the position of the arobas in the email address, and substr(emailaddress, 1, locate(...) - 1) extracts everything before that. Then, we can check if the username starts with that part of the email address using like with a wildcard on the right side.

Find all entries that contain more than one colon character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to find all entries that contain more than one colon (:) character.
However when I do LIKE %:% it shows the entire table because of http://. How can I find more than one colon?
SELECT *
FROM `downloads`
WHERE `url` LIKE '%:%'
LIMIT 0 , 30
If you want to find a colon that occurs after the scheme of your URL, then change your LIKE clause accordingly:
SELECT *
FROM `downloads`
WHERE `url` LIKE '%:%:%'
LIMIT 0 , 30
The first colon will be in your scheme, and the second will be somewhere else in the Url after the scheme.
A word of caution, however - it is completely valid to have a colon in the Url when a port number is specified, e.g.: http://localhost:8080

Append $ character to a MySQL DB field so that length is increased by 40 % [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to append any character, say $, to the name of the student field in database such that its length is increased by 40 %, and save the result in the database.
E.g.:
Name: VijayKumar
After update:
Name: VijayKumar$$$$
You would use concat() and repeat():
select concat(name, repeat('$', ceil(length(name) * 0.4))

how to select the result using REGEXP in mysql,not contain a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
MySQL does not support the Negative Lookahead. How can I find the result not containing a string using REGEXP.
I am using 'NOT REGEXP' but the result is unexpected.
there is a 'Content' column in my table,i want to find the rows which the Content column contains '' label,but i still want some src to be excluded.
here is the sql:
Content REGEXP '.' AND Content NOT REGEXP '.(test.mywebsite1.com/|img.mywebsite.com/face/|test.mywebsite.com/phoneIcon.jpg).*'
but when the Content contain both and it works unexpected;
Test your REGEXP on a known set, get that working, and verify it is working.
Then add the NOT to get the boolean inverse.
Note that a MySQL boolean expression will return one of three possible values: TRUE, FALSE and NULL.
And note that NOT expr will also return one of three possible values: TRUE, FALSE and NULL.
When expr returns NULL, then NOT expr will also return NULL.
It's not really productive to attempt to provide any other assistance, absent an actual question and more details of what you are attempting to do.