简体   繁体   English

SQL更新列为大写小写

[英]Sql update column to upper lower case

I have a column called [strCustomerName] 我有一列名为[strCustomerName]

This column has values like 'ray mantle', 'JAMES MAY', 'john DOE', 'RICHARD & Judy Miller' and so on... 该列的值包括“雷罩”,“詹姆斯·梅”,“约翰·DOE”,“理查德和朱迪·米勒”等等。

I need to do an Update to the table to correct the case of all values in the selected column. 我需要对表进行更新以更正所选列中所有值的大小写。 The Update would then set correctly the values case to 'Ray Mantle', 'James May', 'John Doe', 'Richard & Judy Miller' 然后,更新程序将正确设置值的大小写为“ Ray Mantle”,“ James May”,“ John Doe”,“ Richard&Judy Miller”

Any help doing with SQL would be great. 与SQL有关的任何帮助都将非常有用。 Thanks 谢谢

I would just create a function to convert your string (or names in your scenario) to CAMEL case something like below: 我只要创建一个函数即可将您的字符串(或您的方案中的名称)转换为CAMEL情况,如下所示:

CREATE FUNCTION [dbo].[CamelCase]
(@Str varchar(8000))
RETURNS varchar(8000) AS
BEGIN
  DECLARE @Result varchar(2000)
  SET @Str = LOWER(@Str) + ' '
  SET @Result = ''
  WHILE 1=1
  BEGIN
    IF PATINDEX('% %',@Str) = 0 BREAK
    SET @Result = @Result + UPPER(Left(@Str,1))+
    SubString  (@Str,2,CharIndex(' ',@Str)-1)
    SET @Str = SubString(@Str,
      CharIndex(' ',@Str)+1,Len(@Str))
  END
  SET @Result = Left(@Result,Len(@Result))
  RETURN @Result
END;

Once your function is created, you could then use it in your update statement as below: 创建函数后,可以在更新语句中使用它,如下所示:

UPDATE Customer
SET strCustomerName = dbo.CamelCase(strCustomerName)

You could also call the above function in the select statement directly to check if the values are going to update correctly before running the update statement above: 您还可以直接在select语句中调用上述函数,以在运行上述update语句之前检查值是否正确更新:

select id
      ,dbo.CamelCase(strCustomerName) as CustomerName
from Customer;

Hope this helps! 希望这可以帮助!

SQL Fiddle Demo SQL小提琴演示

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

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