我有一个表作为所有UPPER CASE导入,我想把它变成正确的案例.你们有什么脚本用来完成这个?
这个功能:
"正确案例"所有由白色空间划分的"大写"字样
单独留下"小写字"
即使对于非英文字母也能正常工作
是可移植的,因为它不使用最新SQL Server版本的花哨功能
可以轻松更改为使用NCHAR和NVARCHAR获得unicode支持,以及您认为合适的任何参数长度
可以配置空白区域定义
CREATE FUNCTION ToProperCase(@string VARCHAR(255)) RETURNS VARCHAR(255) AS BEGIN DECLARE @i INT -- index DECLARE @l INT -- input length DECLARE @c NCHAR(1) -- current char DECLARE @f INT -- first letter flag (1/0) DECLARE @o VARCHAR(255) -- output string DECLARE @w VARCHAR(10) -- characters considered as white space SET @w = '[' + CHAR(13) + CHAR(10) + CHAR(9) + CHAR(160) + ' ' + ']' SET @i = 1 SET @l = LEN(@string) SET @f = 1 SET @o = '' WHILE @i <= @l BEGIN SET @c = SUBSTRING(@string, @i, 1) IF @f = 1 BEGIN SET @o = @o + @c SET @f = 0 END ELSE BEGIN SET @o = @o + LOWER(@c) END IF @c LIKE @w SET @f = 1 SET @i = @i + 1 END RETURN @o END
结果:
dbo.ToProperCase('ALL UPPER CASE and SOME lower ÄÄ ÖÖ ÜÜ ÉÉ ØØ ?? ÆÆ') ----------------------------------------------------------------- All Upper Case and Some lower Ää Öö Üü Éé Øø Cc Ææ
这是一个可以解决问题的UDF ......
create function ProperCase(@Text as varchar(8000)) returns varchar(8000) as begin declare @Reset bit; declare @Ret varchar(8000); declare @i int; declare @c char(1); if @Text is null return null; select @Reset = 1, @i = 1, @Ret = ''; while (@i <= len(@Text)) select @c = substring(@Text, @i, 1), @Ret = @Ret + case when @Reset = 1 then UPPER(@c) else LOWER(@c) end, @Reset = case when @c like '[a-zA-Z]' then 0 else 1 end, @i = @i + 1 return @Ret end
您仍然必须使用它来更新您的数据.
UPDATE titles SET title = UPPER(LEFT(title, 1)) + LOWER(RIGHT(title, LEN(title) - 1))
http://sqlmag.com/t-sql/how-title-case-column-value
如果您可以在SQL Server中启用CLR(需要2005或更高版本),那么您可以创建一个CLR函数,该函数使用TextInfo.ToTitleCase内置函数,这将允许您创建一种文化感知方式,只需几个代码行.
我在游戏中有点晚了,但我相信它更实用,它适用于任何语言,包括俄语,德语,泰语,越南语等.它会在'或 - 之后做任何大写.或(或)或空间(显然:).
CREATE FUNCTION [dbo].[fnToProperCase]( @name nvarchar(500) ) RETURNS nvarchar(500) AS BEGIN declare @pos int = 1 , @pos2 int if (@name <> '')--or @name = lower(@name) collate SQL_Latin1_General_CP1_CS_AS or @name = upper(@name) collate SQL_Latin1_General_CP1_CS_AS) begin set @name = lower(rtrim(@name)) while (1 = 1) begin set @name = stuff(@name, @pos, 1, upper(substring(@name, @pos, 1))) set @pos2 = patindex('%[- ''.)(]%', substring(@name, @pos, 500)) set @pos += @pos2 if (isnull(@pos2, 0) = 0 or @pos > len(@name)) break end end return @name END GO
我知道这是在这个帖子中的后期但是,值得一看.这个功能对我有用.所以想分享它.
CREATE FUNCTION [dbo].[fnConvert_TitleCase] (@InputString VARCHAR(4000) ) RETURNS VARCHAR(4000) AS BEGIN DECLARE @Index INT DECLARE @Char CHAR(1) DECLARE @OutputString VARCHAR(255) SET @OutputString = LOWER(@InputString) SET @Index = 2 SET @OutputString = STUFF(@OutputString, 1, 1,UPPER(SUBSTRING(@InputString,1,1))) WHILE @Index <= LEN(@InputString) BEGIN SET @Char = SUBSTRING(@InputString, @Index, 1) IF @Char IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&','''','(') IF @Index + 1 <= LEN(@InputString) BEGIN IF @Char != '''' OR UPPER(SUBSTRING(@InputString, @Index + 1, 1)) != 'S' SET @OutputString = STUFF(@OutputString, @Index + 1, 1,UPPER(SUBSTRING(@InputString, @Index + 1, 1))) END SET @Index = @Index + 1 END RETURN ISNULL(@OutputString,'') END
测试电话:
select dbo.fnConvert_TitleCase(Upper('ÄÄ ÖÖ ÜÜ ÉÉ ØØ ?? ÆÆ')) as test select dbo.fnConvert_TitleCase(upper('Whatever the mind of man can conceive and believe, it can achieve. – Napoleon hill')) as test
结果: