简体   繁体   English

选择所有在特定列中具有全大写字符串的表条目?

[英]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.

相关问题 从一个表中选择所有条目,在另一个表中有两个特定条目 - Select all entries from one table which has two specific entries in another table 从表中选择所有早于3天的条目 - Select all entries from a table which are older then 3 days 从表B获取所有与表A的多个条目(给定列表)相关的条目 - Get all entries from Table B which have a relation to multiple entries (given list) from Table A SQL查询:如何选择相关表中所有记录具有特定属性值的记录 - SQL query : how to select records which all records in related table have specific value of attribute 为B列中的唯一条目选择具有A列最大值的行 - Select rows which have the maximum values of column A for unique entries in column B 让mysql select语句返回完全合格的列名,如table.field - have mysql select statement return fully qualified column names like table.field 如何选择所有带有特定标签的帖子? - How can I select all posts which have specific tags? PostgreSQL:选择具有与另一个表中的所有条目对应的条目的所有类型 - PostgreSQL: select all types that have an entry corresponding to all entries in another table 如何选择某些列中具有相同值的所有行 - How to select all rows which have same value in some column SQL 选择所有日期范围内都存在的特定列 - SQL select specific column which are present for all date range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM