[英]Select all table entries which have a fully capitalized string in a specific column?
I have a database table with a few thousand entries. 我有一个包含数千个条目的数据库表。 A part of the entries (~20%) have been entered with a fully capitalized strings in the 'name' column. 部分条目(约20%)已在“名称”列中输入了全大写的字符串。
Example: 例:
id | name
---------
1 | THOMAS GOLDENBERG
2 | Henry Samuel
3 | GIL DOFT
4 | HARRY CRAFT
5 | Susan Etwall
6 | Carl Cooper
How would an SQL query look like that selects all entries with a fully capitalized string in the name column? 这样的SQL查询将如何在name列中选择所有具有完全大写字符串的条目? (ie in the example: those with the ID 1,3,4) (即在示例中:ID为1,3,4的ID)
In MySQL it would be: 在MySQL中,它将是:
SELECT id FROM table WHERE name = UPPER(name);
I think this would work the same way in SQL Server, DB2 and Postgres. 我认为这在SQL Server,DB2和Postgres中将以相同的方式工作。
What database system? 什么数据库系统?
In theory you can do a simple SELECT ... WHERE name = UPPER(name);
理论上,您可以执行简单的SELECT ... WHERE name = UPPER(name);
but that does not always work. 但这并不总是有效。 Depending on the collation of your data, you may found that all records satisfy this condition because the comparison used may be case insensitive. 根据数据的排序规则,您可能会发现所有记录都满足此条件,因为所使用的比较可能不区分大小写。
You need to ensure you compare using a case sensitive collation, and the correct answer depends on the database platform you use. 您需要确保使用区分大小写的排序规则进行比较,正确的答案取决于您使用的数据库平台。 For example, using SQL Server syntax: 例如,使用SQL Server语法:
SELECT ... WHERE Name COLLATE Latin1_General_100_CS_AS = UPPER(Name);
This also works in MySQL with the condition that you use a collation name valid on MySQL. 这在MySQL中也可以使用,条件是您使用在MySQL上有效的排序规则名称。
select * from your_table where name = upper(name)
Here's a MySql function to convert uppercase to title case: 这是一个MySql函数,用于将大写转换为标题大小写:
example: 例:
update your_table set name = tcase(name) where name = upper(name);
function: 功能:
CREATE FUNCTION `tcase`(str text) RETURNS text CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE result TEXT default '';
DECLARE space INT default 0;
DECLARE last_space INT default 0;
IF (str IS NULL) THEN
RETURN NULL;
END IF;
IF (char_length(str) = 0) THEN
RETURN '';
END IF;
SET result = upper(left(str,1));
SET space = locate(' ', str);
WHILE space > 0 DO
SET result = CONCAT(result, SUBSTRING(str, last_space+2, space-last_space-1));
SET result = CONCAT(result, UPPER(SUBSTRING(str, space+1, 1)));
SET last_space = space;
SET space = locate(' ', str, space+2);
END WHILE;
SET result = CONCAT(result, SUBSTRING(str, last_space+2));
RETURN result;
END $$
DELIMITER ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.