mysql: Count number of occurances of uppercase-letters in a string - mysql

I have a MySQL-Database with a column mystring varchar(255).
What I want to know is the occurences of standard latin uppercase letters ('A' to 'Z').
So for example "AbcDeF" countains 3 upperscase letters and "XYZZ" contains 4.
Basically I would like to calculate the value in the database, something in the form of:
SELECT count_characters(mystring, 'A', 'Z') FROM mytable;
Is it possible in MySQL?

This result can be returned from MySQL. It's not impossible, but it's downright ugly. To make this prettier, you need to hide the ugliness in a user defined function.
You can use the REPLACE function to do a case sensitive search, to replace a specific character with a zero length string. Repeat that for each specific character you want to count. Then get the character length of the resulting string, and subtract that from the character length of the original string. The difference is the number of characters that were replaced (which was the number of characters you wanted to count).
As an example, to get a count of the uppercase letters 'A' thru 'D' from a particular column, for each row in your table...
SELECT CHAR_LENGTH(t.mycol) -
CHAR_LENGTH(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
t.mycol
,'A','')
,'B','')
,'C','')
,'D','')
) AS count_characters
FROM mytable t
It's ugly, but it will return the specified result.
NOTE: The LENGTH function would work for the latin characters 'A' thru 'Z', but we use the CHAR_LENGTH function in anticipation of handling multi-byte UTF encodings.

Related

MS SQL Query to remove prefixes

I have below like column values and would like to exclude the characters as well as the hyphen and only return digits. The replace function is not entirely helpful as sometimes the character length is 3 and sometimes its 4, see below as the digit length changes as well.
abc-1234567
sdfr-9876540
try-12345678
case-098765
If you want the part after the last hyphen, you can use substring_index():
select substring_index(col, '-', -1)
You can also extract the digits at the end using regexp_substr():
select regexp_substr(col, '[0-9]+$')

MySQL command to get first letter of last name

Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'

match second character regex mysql

I was watching this mysql course where it the following example was given:
SELECT Name, Continent, Population FROM Country WHERE Name LIKE '_%a' ORDER BY Name;
And they said that '_a%' would match all strings in the Name column whose second character is a. I'm using MariaDB server 10.0.34 on Ubuntu and in my case, the result is quite different. Instead, it shows all strings in the Name column who end in a. Any idea why that is and where the difference exists?
Thanks.
Ummm. A couple of points.
1) those are not regular expressions, those are LIKE comparisons. (Yeah, yeah, to-may-toh, tah-mah-toh, I know.) But we can be precise in our terminology, and avoid confusion and obfuscation.
2) '_%a' and '_a%' are significantly different, as your own observations have revealed
_ underscore matches any one character
% percent matches zero, one or more of any character
a matches the character 'a'
So
LIKE '_a%' matches any single character, followed by an 'a', followed by any number of (zero, one or more) characters
LIKE '_%a' matches any single character, followed by any number of (zero, one or more) any characters, and ending with an 'a'
As a demonstration:
SELECT 'name' LIKE '_a%' -- true - at least two chars, second char is a
, 'name' LIKE '_%a' -- false - at least two chars, last char is a
, 'name' LIKE '_e%' -- false - at least two chars, second char is e
, 'name' LIKE '_%e' -- true - at least two chars, last char is e
Those are LIKE comparisons. To do the equivalent using regular expression, something like this:
SELECT 'name' REGEXP '^.a' -- at least two chars, second char is a
, 'name' REGEXP '^..*a$' -- at least two chars, last char is a
, 'name' RLIKE '^.e' -- at least two chars, second char is e
, 'name' RLIKE '^..*e$' -- at least two chars, last char is e

How to use SQL to remove superfluous characters from names?

How do I remove all superfluous full-stop . and semi-colon ; characters from end of last name field values in SQL?
One way to check of the last character is a "full stop" or "semicolon" is to use a substring function to get the last character, and compare that to the characters you are looking for. (There are several ways to do this, for example, using LIKE or REGEXP operator.
If that last character matches, then lop off that last character. One way to do that is to use a substring function. (Use the CHAR_LENGTH function to return the number of characters in the string.)
For example, something like this:
UPDATE mytable t
SET t.last_name = SUBSTR(t.last_name,1,CHAR_LENGTH(t.last_name)-1)
WHERE SUBSTRING(t.last_name,CHAR_LENGTH(t.last_name),1) IN ('.',';')
But, I'd strongly recommend that you test those expressions using a SELECT statement, before running an UPDATE statement.
SELECT t.last_name AS old_val
, SUBSTR(t.last_name,1,CHAR_LENGTH(t.last_name)-1) AS new_val
FROM mytable t
WHERE SUBSTRING(t.last_name,CHAR_LENGTH(t.last_name),1) IN ('.',';')
Substring rows that have a semi-colon or dot :
update emp
set ename = substring(ename, 1, char_length(ename) - 1)
where ename REGEXP '[.;]$';

Select trims spaces from strings - is this a bug or in the spec?

in mysql:
select 'a' = 'a ';
return 1
You're not the first to find this frustrating. In this case, use LIKE for literal string comparison:
SELECT 'a' LIKE 'a '; //returns 0
This behavior is specified in SQL-92 and SQL:2008. For the purposes of comparison, the shorter string is padded to the length of the longer string.
From the draft (8.2 <comparison predicate>):
If the length in characters of X is not equal to the length in characters of Y, then the shorter string is effectively replaced, for the purposes of comparison, with a copy of itself that has been extended to the length of the longer string by concatenation on the right of one or more pad characters, where the pad character is chosen based on CS. If CS has the NO PAD characteristic, then the pad character is an implementation-dependent character different from any character in the character set of X and Y that collates less than any string under CS. Otherwise, the pad character is a <space>.
In addition to the other excellent solutions:
select binary 'a' = 'a '
I googled for "mysql string" and found this:
In particular, trailing spaces [using LIKE] are significant, which is not true for CHAR or VARCHAR comparisons performed with the = operator
From the documentation:
All MySQL collations are of type PADSPACE. This means that all CHAR and VARCHAR values in MySQL are compared without regard to any trailing spaces
The trailing spaces are stored in VARCHAR in MySQL 5.0.3+:
CREATE TABLE t_character (cv1 CHAR(10), vv1 VARCHAR(10), cv2 CHAR(10), vv2 VARCHAR(10));
INSERT
INTO t_character
VALUES ('a', 'a', 'a ', 'a ');
SELECT CONCAT(cv1, cv1), CONCAT(vv2, vv1)
FROM t_character;
but not used in comparison.
Here's another workaround that might help:
select 'a' = 'a ' and length('a') = length('a ');
returns 0