SQL查询使列中的所有数据都大写?


80

我需要一个SQL查询来使列中的所有数据都大写吗?

有任何想法吗?

Answers:


189

常驻:

UPDATE
  MyTable
SET
  MyColumn = UPPER(MyColumn)

临时:

SELECT
  UPPER(MyColumn) AS MyColumn
FROM
  MyTable

16

如果只想更新当前不是大写的行(而不是所有行),则需要使用以下方法来识别差异COLLATE

UPDATE MyTable
SET    MyColumn = UPPER(MyColumn)
WHERE  MyColumn != UPPER(MyColumn) COLLATE Latin1_General_CS_AS 

有关整理的一些知识

区分大小写取决于您的排序规则设置,默认情况下通常不区分大小写。

可以在服务器,数据库,列或查询级别设置排序规则:

-- Server
SELECT SERVERPROPERTY('COLLATION')
-- Database
SELECT name, collation_name FROM sys.databases
-- Column 
SELECT COLUMN_NAME, COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE CHARACTER_SET_NAME IS NOT NULL

排序规则名称指定应如何编码和读取字符串,例如:

  • Latin1_General_CI_AS →不区分大小写
  • Latin1_General_CS_AS →区分大小写
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.