简体   繁体   English

如何在sql中进行区分大小写的比较?

[英]How do I do a case sensitive comparison in sql?

Just started a tutorial in SQL for beginners. 刚为初学者启动了SQL教程。 I'm doing some exercises now and I would like to know how to change the title. 我现在正在做一些练习,我想知道如何更改标题。

If you look at here: you'll see that I have made firstname, lastname, title, age and salary. 如果你看这里:你会看到我已经取得了名字,姓氏,头衔,年龄和薪水。 And I wrote the letters in small letter. 我用小写字母写了这些信。 How can I change it to capital letter? 如何将其更改为大写字母?

http://tinypic.com/r/amtpgm/3 http://tinypic.com/r/amtpgm/3

I tried using this: 我试过用这个:

update mytablename 
set firstname = 'Firstname'
where firstname = 'firstname'

But I later realized that this one will not work. 但我后来意识到这个不行。

Thanks 谢谢

==== ====

additional question: 其他问题:

I also notice that if I wrote with spaces, then its not recognized. 我也注意到,如果我用空格写,那么它不被认可。 It's the first part only which will be displayed. 这是仅显示的第一部分。 Do you know why is it doing? 你知道它为什么这么做吗? Thanks 谢谢

create table myemployees_tr0214
(First Name varchar(20),
Last Name varchar(20),
Title char(5),
Age number(3),
Salary number(6,10));

========== ==========

thank you for all your inputs. 谢谢你的所有投入。 I've tried this one in renaming the "Firstname" to "Fname" and it didn't work. 我已经尝试过这个将“名字”重命名为“Fname”并且它不起作用。 Did I miss something? 我错过了什么?

alter table myemployees_tr0214
rename column Firstname to Fname;

This should update all the firstnames in the table to an uppercase first letter: 这应该将表中的所有firstnames更新为大写的第一个字母:

UPDATE mytablename SET firstname = CONCAT(UCASE(MID(firstname,1,1)),MID(firstname,2));

Hope this helps you :) 希望这可以帮助你:)

First, unless you really want to change the names of the fields, don't. 首先,除非你真的想要改变字段的名称,否则不要。 It's not really all that important if all you're doing is learning SQL. 如果你所做的只是学习SQL,那真的不是那么重要。 Note that if you want to learn the syntax for doing so, then of course it would be a worthwhile exercise, but other than that, I'd let it be. 请注意,如果你想学习这样做的语法,那么当然这将是一个值得的练习,但除此之外,我会让它成为现实。

Your edited question mentions using spaces in names. 您编辑的问题提到在名称中使用空格。 This is not allowed. 这是不允许的。 The rules for what constitutes a "SQL Identifier", be it the name of a table, column, constraint, etc. has some strict rules, and simplified they are that you should only use letters, underscores, and digits, except that you can't start with a digit. 什么构成“SQL标识符”的规则,无论是表,列,约束等的名称都有一些严格的规则,简化它们是你应该只使用字母,下划线和数字,除了你可以从一个数字开始。

Now, why the online website that you're using to learn SQL through doesn't complain when you add those spaces, that I don't know, and to me that makes it a little suspect. 现在,为什么你用来学习SQL的在线网站在你添加这些空间时不会抱怨,我不知道,而且对我来说这让人有点怀疑。 It doesn't sound as though it actually uses a known database engine, as just the presence of those spaces there + the extra words would make any normal database engine complain about bad syntax. 听起来好像它实际上使用的是已知的数据库引擎,因为只有那些空间的存在+额外的单词会使任何普通的数据库引擎抱怨错误的语法。

In order to fix it, either add underscores instead of spaces, or contract and use camelCasing, like this: FirstName, LastName 为了修复它,要么添加下划线而不是空格,要么合同并使用camelCasing,如下所示:FirstName,LastName

update mytablename set firstname = 'Firstname'; where firstname = 'firstname';

This will update the values of the firstname column. 这将更新firstname列的 What you are trying to do is change the name of the firstname column. 您要做的是更改firstname列的名称。 How to do this depends on the database you're using (which you haven't mentioned). 如何执行此操作取决于您正在使用的数据库(您没有提到过)。

If it's MS Access or SQL server, you can open the table in the UI and use the designer to change the column name. 如果是MS Access或SQL Server,则可以在UI中打开表并使用设计器更改列名。

Otherwise you can use the SQL ALTER TABLE statement, as described here . 否则,你可以使用SQL ALTER TABLE语句,描述在这里

For MS SQL Server... 对于MS SQL Server ...

You'd use [ and ] to delimit identifiers: 您可以使用[和]来分隔标识符:

create table myemployees_tr0214 (
  [First Name] varchar(20),  --here
  [Last Name] varchar(20),  --here
  Title char(5),
  Age number(3),
  Salary number(6,10)
);

If you want to change the column name from "firstname" to "Firstname", you could use sp_rename in MS SQL Server. 如果要将列名称从“firstname”更改为“Firstname”,则可以在MS SQL Server中使用sp_rename。

If you want to change the first letter of the data in the "firstname" column, other posters have offered solutions and here's another for a single name. 如果要更改“firstname”列中数据的第一个字母,其他海报提供了解决方案,而另一个海报提供了单个名称。

update mytablename 
set firstname = 'Firstname'
where firstname COLLATE Latin1_general_Bin = 'firstname' COLLATE Latin1_general_Bin

If you're using mysql see: http://dev.mysql.com/doc/refman/5.0/en/charset-binary-op.html 如果您使用的是mysql,请参阅: http//dev.mysql.com/doc/refman/5.0/en/charset-binary-op.html

The BINARY operator casts the string following it to a binary string. BINARY运算符将其后面的字符串转换为二进制字符串。 This is an easy way to force a comparison to be done byte by byte rather than character by character. 这是一种简单的方法,可以逐个字节而不是逐个字符地强制进行比较。 BINARY also causes trailing spaces to be significant BINARY也会导致尾随空格很重要

mysql> SELECT 'a' = 'A';
        -> 1
mysql> SELECT BINARY 'a' = 'A';
        -> 0
mysql> SELECT 'a' = 'a ';
        -> 1
mysql> SELECT BINARY 'a' = 'a ';
        -> 0

Ah, there are two ways to read this question. 啊,有两种方法可以阅读这个问题。 The first is based on reading the sample UPDATE you posted. 第一个是基于阅读您发布的样本更新。 This will fail because SQL by default doesn't do a case sensitive comparison on strings. 这将失败,因为SQL默认情况下不对字符串进行区分大小写的比较。

The second piece of code implies what you wanted was to ALTER TABLE and change the name of the column from a column name with one casing to another. 第二段代码暗示您想要的是ALTER TABLE并将列的名称从一个外壳更改为另一个外壳。 In MS-SQL, you can't do that without dropping the whole table and re-creating it, in other dialects of SQL there will be version specific DDL syntax. 在MS-SQL中,如果不删除整个表并重新创建它,就不能这样做,在SQL的其他方言中,将有特定于版本的DDL语法。

And finally, in MS-Access, if a column name has a space, you wrap it in double quotes, eg "My Column" in SQL wrap it in [My Column] 最后,在MS-Access中,如果列名有空格,则用双引号括起来,例如SQL中的“My Column”将其包装在[My Column]中

you'll need some kind of unique row indentifier like id so you can do 你需要某种独特的行标识符,如id,所以你可以这样做

update mytablename set firstname = 'Firstname' where id = 1

now what can be used as a unique row indentifier is a huge debate of natural vs surrogate keys. 现在什么可以用作独特的行标识符是自然代理键的巨大争论。 use what you think is best for your example but i'm a supporter of surogate keys since every natural key has the possibility to change. 使用你认为最适合你的例子,但我是surogate键的支持者,因为每个自然键都有可能改变。

This may need to be optimize for more but it will allow you to even update mulitple first name MSSQL version 这可能需要针对更多进行优化,但它将允许您甚至更新多个名字MSSQL版本

select 'mary ellen' as firstname into #test
insert into #test select 'Nathan'


select 
Case when patindex('% %',firstname) >0 then
upper(left(firstname,1)) --first letter
+ rtrim(substring(firstname,2,patindex('% %',firstname)-1)) --whole firstname
+ ' ' -- space
+ Upper(substring(firstname,patindex('% %',firstname)+1,1)) --first letter last name
+  rtrim(substring(firstname,patindex('% %',firstname)+2, len(firstname)))
else
upper(left(firstname,1)) + substring(firstname,2,len(firstname))
end as firstname
from #test


update #test
set firstname = Case when patindex('% %',firstname) >0 then
upper(left(firstname,1)) --first letter
+ rtrim(substring(firstname,2,patindex('% %',firstname)-1)) --whole firstname
+ ' ' -- space
+ Upper(substring(firstname,patindex('% %',firstname)+1,1)) --first letter last name
+  rtrim(substring(firstname,patindex('% %',firstname)+2, len(firstname)))
else
upper(left(firstname,1)) + substring(firstname,2,len(firstname))
end 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM